KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > osgi > server > Activator


1 package com.scalagent.joram.osgi.server;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.osgi.framework.*;
7
8 import com.scalagent.joram.osgi.server.service.JoramAdmin;
9
10 import org.objectweb.joram.client.jms.ConnectionFactory;
11 import org.objectweb.joram.client.jms.local.LocalConnectionFactory;
12 import org.objectweb.joram.client.jms.admin.AdminModule;
13 import org.objectweb.joram.client.jms.admin.User;
14 import org.objectweb.joram.client.jms.Queue;
15 import org.objectweb.joram.client.jms.Topic;
16
17 import fr.dyade.aaa.agent.AgentServer;
18 import fr.dyade.aaa.agent.*;
19 import fr.dyade.aaa.agent.conf.*;
20
21 public class Activator implements BundleActivator {
22
23   private static BundleContext bcontext;
24
25   public static BundleContext getBundleContext() {
26     return bcontext;
27   }
28
29   /**
30    * Implements BundleActivator.start(). Initialize and starts the
31    * AgentServer runtime.
32    *
33    * @param context the framework context for the bundle.
34    */

35   public void start(BundleContext context) throws Exception JavaDoc {
36     bcontext = context;
37
38     startAgentServer();
39
40     Properties props = new Properties();
41     context.registerService(JoramAdmin.class.getName(),
42                             new JoramAdminImpl(),
43                             props);
44   }
45
46   private void startAgentServer() throws Exception JavaDoc {
47     //search a3server.prop in path used to load classes.
48
ClassLoader JavaDoc classLoader = getClass().getClassLoader();
49     InputStream is = classLoader.getResourceAsStream("a3server.prop");
50     Properties a3prop = new Properties();
51     a3prop.load(is);
52
53     String JavaDoc serverId = a3prop.getProperty("sid");
54     String JavaDoc path = a3prop.getProperty("path");
55     String JavaDoc relativePath = a3prop.getProperty("relativePath");
56     String JavaDoc storageDirName = a3prop.getProperty("storageDirName");
57     String JavaDoc logicalServerName = a3prop.getProperty("logicalServerName");
58
59     File storageDir = new File(new File(path), storageDirName);
60     File lockFile = new File(storageDir, "lock");
61     lockFile.delete();
62
63     // find the most precise debug file
64
if (logicalServerName != null) try {
65       String JavaDoc debugDir = System.getProperty(
66         Debug.DEBUG_DIR_PROPERTY, Debug.DEFAULT_DEBUG_DIR);
67       File debugFile = new File(debugDir, logicalServerName);
68       if ((debugFile != null) &&
69           debugFile.exists() &&
70           debugFile.isDirectory()) {
71         debugDir = debugFile.getPath();
72         String JavaDoc debugFileName = System.getProperty(
73           Debug.DEBUG_FILE_PROPERTY, Debug.DEFAULT_DEBUG_FILE);
74         debugFile = new File(debugFile, debugFileName);
75         if ((debugFile != null) &&
76             debugFile.exists() &&
77             debugFile.isFile() &&
78             (debugFile.length() != 0)) {
79           Debug.setDebugDir(debugDir);
80           Debug.setDebugFileName(debugFileName);
81         }
82       }
83     } catch (Exception JavaDoc exc) {
84     }
85
86     AgentServer.init(new String JavaDoc[] {
87       serverId,
88       storageDir.getAbsolutePath()});
89     AgentServer.start();
90
91   }
92
93   /**
94    * Implements BundleActivator.stop(). Stops the AgentServer runtime.
95    *
96    * @param context the framework context for the bundle.
97    */

98   public void stop(BundleContext context) {
99     stopAgentServer();
100   }
101
102   private void stopAgentServer() {
103     AgentServer.stop();
104     AgentServer.reset();
105   }
106
107   public static class JoramAdminImpl implements JoramAdmin {
108
109     private String JavaDoc rootUserName= "root";
110
111     public void setRootUserName(String JavaDoc root) {
112       this.rootUserName = root;
113     }
114
115     private String JavaDoc rootPassword = "root";
116
117     public void setRootPassword(String JavaDoc password) {
118       this.rootPassword = password;
119     }
120
121     /**
122      *
123      */

124     public ConnectionFactory getLocalConnectionFactory() throws Exception JavaDoc {
125       return (ConnectionFactory) LocalConnectionFactory.create();
126     }
127
128     /**
129      *
130      */

131     public boolean executeAdminXML(Reader reader) throws Exception JavaDoc {
132       return AdminModule.executeAdmin(reader);
133     }
134
135     /**
136      * Creates or retrieves a user on the underlying JORAM server.
137      *
138      * @exception Exception If the creation fails.
139      */

140     public void createUser(String JavaDoc name, String JavaDoc password) throws Exception JavaDoc {
141       try {
142         AdminModule.collocatedConnect(rootUserName, rootPassword);
143         User.create(name, password);
144       } finally {
145         AdminModule.disconnect();
146       }
147     }
148
149     /**
150      * Creates or retrieves a queue destination on the underlying JORAM server,
151      * (re)binds the corresponding <code>Queue</code> instance.
152      *
153      * @param name The name of the queue.
154      *
155      * @exception Exception If the creation fails.
156      */

157     public Queue createQueue(String JavaDoc name) throws Exception JavaDoc {
158       Queue queue = null;
159       try {
160         AdminModule.collocatedConnect(rootUserName, rootPassword);
161         queue = Queue.create(name);
162         queue.setFreelyWriteable(true);
163         queue.setFreelyReadable(true);
164       } finally {
165         AdminModule.disconnect();
166       }
167       return queue;
168     }
169
170     /**
171      * Creates or retrieves a topic destination on the underlying JORAM server,
172      * (re)binds the corresponding <code>Topic</code> instance.
173      *
174      * @exception Exception If the creation fails.
175      */

176     public Topic createTopic(String JavaDoc name) throws Exception JavaDoc {
177       Topic topic = null;
178       try {
179         AdminModule.collocatedConnect(rootUserName, rootPassword);
180         topic = Topic.create(name);
181         topic.setFreelyWriteable(true);
182         topic.setFreelyReadable(true);
183       } finally {
184         AdminModule.disconnect();
185       }
186       return topic;
187     }
188
189     /**
190      * Returns the list of all destinations that exist on the server.
191      */

192     public List getDestinations() throws Exception JavaDoc {
193       List destinations = null;;
194       try {
195         AdminModule.collocatedConnect(rootUserName, rootPassword);
196         destinations = AdminModule.getDestinations();
197       } finally {
198         AdminModule.disconnect();
199       }
200       return destinations;
201     }
202
203     /**
204      * Returns the list of all users that exist on a given server.
205      */

206     public List getUsers() throws Exception JavaDoc {
207       List users = null;;
208       try {
209         AdminModule.collocatedConnect(rootUserName, rootPassword);
210         users = AdminModule.getUsers();
211       } finally {
212         AdminModule.disconnect();
213       }
214       return users;
215     }
216   }
217 }
218
Popular Tags