KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > carol > CarolComponent


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CarolComponent.java 1148 2006-10-11 08:27:36Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.carol;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL;
29
30 import java.io.File JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.objectweb.carol.jndi.ns.NameServiceException;
38 import org.objectweb.carol.jndi.ns.NameServiceManager;
39 import org.objectweb.carol.util.configuration.ConfigurationException;
40 import org.objectweb.carol.util.configuration.ConfigurationRepository;
41 import org.objectweb.easybeans.component.api.EZBComponentException;
42 import org.objectweb.easybeans.component.itf.RegistryComponent;
43 import org.objectweb.easybeans.log.JLog;
44 import org.objectweb.easybeans.log.JLogFactory;
45
46 /**
47  * This component allows to start a registry.
48  * @author Florent Benoit
49  */

50 public class CarolComponent implements RegistryComponent {
51
52     /**
53      * Logger.
54      */

55     private JLog logger = JLogFactory.getLog(CarolComponent.class);
56
57     /**
58      * Default prefix protocol.
59      */

60     private static final String JavaDoc DEFAULT_PREFIX_PROTOCOL = "rmi";
61
62     /**
63      * List of protocols.
64      */

65     private List JavaDoc<Protocol> protocols = null;
66
67     /**
68      * Creates a new Carol component.
69      */

70     public CarolComponent() {
71         protocols = new ArrayList JavaDoc<Protocol>();
72     }
73
74     /**
75      * Init method.<br/>
76      * This method is called before the start method.
77      * @throws EZBComponentException if the initialization has failed.
78      */

79     public void init() throws EZBComponentException {
80         // check
81
if (protocols == null || protocols.isEmpty()) {
82             logger.info("No protocols, use the existing carol configuration");
83             return;
84         }
85
86         // List of protocols
87
String JavaDoc lstProtocol = null;
88
89         // Create configuration
90
StringBuilder JavaDoc carolConf = new StringBuilder JavaDoc();
91         for (Protocol protocol : protocols) {
92             // Build list of protocols
93
if (lstProtocol != null) {
94                 lstProtocol += ",";
95                 lstProtocol += protocol.getName();
96             } else {
97                 lstProtocol = protocol.getName();
98             }
99
100
101             //Define URL
102
carolConf.append("carol.");
103             carolConf.append(protocol.getName());
104             carolConf.append(".url=");
105             if (protocol.getUrl() != null) {
106                 // set defined URL
107
carolConf.append(protocol.getUrl());
108             } else {
109                 // compute URL
110
String JavaDoc host = protocol.getHostname();
111                 int portNumber = protocol.getPortNumber();
112                 carolConf.append(DEFAULT_PREFIX_PROTOCOL);
113                 carolConf.append("://");
114                 carolConf.append(host);
115                 carolConf.append(":");
116                 carolConf.append(portNumber);
117             }
118             carolConf.append("\n");
119
120         }
121
122         // Set carol mode in server mode (will use fixed port if set)
123
System.setProperty("carol.server.mode", "true");
124         try {
125             // hack
126
Writer JavaDoc fw;
127             File JavaDoc fConf = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name") + "ejb3-carol.properties");
128             try {
129                 fw = new FileWriter JavaDoc(fConf);
130                 fw.write("carol.protocols=");
131                 fw.write(lstProtocol);
132                 fw.write("\n");
133                 fw.write(carolConf.toString());
134                 fw.write("carol.jvm.rmi.local.registry=true");
135                 fw.close();
136             } catch (IOException JavaDoc e) {
137                 e.printStackTrace();
138             }
139             ConfigurationRepository.init(fileToURL(fConf));
140         } catch (ConfigurationException e) {
141             throw new EZBComponentException("Cannot initialize registry", e);
142         }
143
144         try {
145             ConfigurationRepository.addInterceptors("jrmp", "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer");
146         } catch (ConfigurationException e) {
147             throw new EZBComponentException("Cannot add JOTM interceptors", e);
148         }
149
150         try {
151             ConfigurationRepository.addInterceptors("jrmp",
152                     "org.objectweb.easybeans.security.propagation.rmi.jrmp.interceptors.SecurityInitializer");
153         } catch (ConfigurationException e) {
154             throw new EZBComponentException("Cannot add Security interceptors", e);
155         }
156
157     }
158
159
160     /**
161      * Start method.<br/>
162      * This method is called after the init method.
163      * @throws EZBComponentException if the start has failed.
164      */

165     public void start() throws EZBComponentException {
166         // Start registry only if there are protocols
167
if (protocols != null && !protocols.isEmpty()) {
168             try {
169                 NameServiceManager.startNS();
170             } catch (NameServiceException e) {
171                 throw new EZBComponentException("Cannot start registry", e);
172             }
173         }
174     }
175
176
177     /**
178      * Stop method.<br/>
179      * This method is called when component needs to be stopped.
180      * @throws EZBComponentException if the stop is failing.
181      */

182     public void stop() throws EZBComponentException {
183         // Stop registry only if there are protocols
184
if (protocols != null && !protocols.isEmpty()) {
185             try {
186                 NameServiceManager.stopNS();
187             } catch (NameServiceException e) {
188                 throw new EZBComponentException("Cannot stop the registry", e);
189             }
190         }
191     }
192
193     /**
194      * Gets the protocols defined for the start.
195      * @return the list of protocols.
196      */

197     public List JavaDoc<Protocol> getProtocols() {
198         return protocols;
199     }
200
201     /**
202      * Sets the list of protocols.
203      * @param protocols the list of protocols configured for this server.
204      */

205     public void setProtocols(final List JavaDoc<Protocol> protocols) {
206         this.protocols = protocols;
207     }
208
209     /**
210      * Gets the default Provider URL.
211      * @return the provider URL that is used by default.
212      */

213     public String JavaDoc getProviderURL() {
214         return ConfigurationRepository.getCurrentConfiguration().getProviderURL();
215     }
216
217 }
218
Popular Tags