KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.scalagent.joram.osgi.client;
2
3 import java.io.*;
4 import java.util.*;
5
6 import org.osgi.framework.*;
7
8 import javax.naming.*;
9
10 import org.objectweb.joram.client.jms.ConnectionFactory;
11 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
12 import org.objectweb.joram.client.jms.local.LocalConnectionFactory;
13 import org.objectweb.joram.client.jms.Queue;
14 import org.objectweb.joram.client.jms.Topic;
15 import org.objectweb.joram.client.jms.admin.User;
16 import org.objectweb.joram.client.jms.admin.AdminModule;
17
18 public class Activator implements BundleActivator {
19
20   private static BundleContext bcontext;
21
22   public static BundleContext getBundleContext() {
23     return bcontext;
24   }
25
26   /**
27    * Implements BundleActivator.start().
28    *
29    * @param context the framework context for the bundle.
30    */

31   public void start(BundleContext context) throws Exception JavaDoc {
32     bcontext = context;
33
34     Properties props = new Properties();
35     context.registerService(
36       com.scalagent.joram.osgi.client.service.JoramClient.class.getName(),
37       new JoramClientImpl(), props);
38   }
39
40   /**
41    * Implements BundleActivator.stop().
42    *
43    * @param context the framework context for the bundle.
44    */

45   public void stop(BundleContext context) {
46   }
47
48   public static class JoramClientImpl
49     implements com.scalagent.joram.osgi.client.service.JoramClient {
50     /**
51      *
52      */

53     public void connect(String JavaDoc host, int port,
54                         String JavaDoc name, String JavaDoc password,
55                         int cnxTimer) throws Exception JavaDoc {
56       AdminModule.connect(host, port, name, password, cnxTimer);
57     }
58     /**
59      *
60      */

61     public void disconnect() {
62       AdminModule.disconnect();
63     }
64
65     /**
66      *
67      */

68     public ConnectionFactory
69         getTcpConnectionFactory(String JavaDoc hostname, int port) throws Exception JavaDoc {
70       return new TcpConnectionFactory(hostname, port);
71     }
72
73     /**
74      *
75      */

76     public Context getInitialContext() throws Exception JavaDoc {
77       Properties prop = new Properties();
78       prop.setProperty("java.naming.factory.initial", "fr.dyade.aaa.jndi2.client.NamingContextFactory");
79       prop.setProperty("java.naming.factory.host", "localhost");
80       prop.setProperty("java.naming.factory.port", "16400");
81       return getInitialContext(prop);
82     }
83
84     /**
85      *
86      */

87     public Context getInitialContext(Hashtable prop) throws Exception JavaDoc {
88       Thread JavaDoc ct = Thread.currentThread();
89       ClassLoader JavaDoc cl = ct.getContextClassLoader();
90       ct.setContextClassLoader(JoramClientImpl.class.getClassLoader());
91       Context ctx = new InitialContext(prop);
92       ct.setContextClassLoader(cl);
93       return ctx;
94     }
95
96     /**
97      *
98      */

99     public boolean executeAdminXML(Reader reader) throws Exception JavaDoc {
100       return AdminModule.executeAdmin(reader);
101     }
102
103     /**
104      * Creates or retrieves a user on the underlying JORAM server.
105      *
106      * @exception Exception If the creation fails.
107      */

108     public void createUser(String JavaDoc name, String JavaDoc password) throws Exception JavaDoc {
109       User.create(name, password);
110     }
111
112     /**
113      * Creates or retrieves a queue destination on the underlying JORAM server,
114      * (re)binds the corresponding <code>Queue</code> instance.
115      *
116      * @param name The name of the queue.
117      *
118      * @exception Exception If the creation fails.
119      */

120     public Queue createQueue(String JavaDoc name) throws Exception JavaDoc {
121       Queue queue = Queue.create(name);
122       queue.setFreelyWriteable(true);
123       queue.setFreelyReadable(true);
124
125       return queue;
126     }
127
128     /**
129      * Creates or retrieves a topic destination on the underlying JORAM server,
130      * (re)binds the corresponding <code>Topic</code> instance.
131      *
132      * @exception Exception If the creation fails.
133      */

134     public Topic createTopic(String JavaDoc name) throws Exception JavaDoc {
135       Topic topic = Topic.create(name);
136       topic.setFreelyWriteable(true);
137       topic.setFreelyReadable(true);
138
139       return topic;
140     }
141
142     /**
143      * Returns the list of all destinations that exist on the server.
144      */

145     public List getDestinations() throws Exception JavaDoc {
146       return AdminModule.getDestinations();
147     }
148
149     /**
150      * Returns the list of all users that exist on a given server.
151      */

152     public List getUsers() throws Exception JavaDoc {
153       return AdminModule.getUsers();
154     }
155   }
156 }
157
Popular Tags