001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id:CarolComponent.java 1477 2007-06-16 16:50:19Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.carol;
025:
026: import static org.ow2.easybeans.util.url.URLUtils.fileToURL;
027:
028: import java.io.File;
029: import java.io.FileWriter;
030: import java.io.IOException;
031: import java.io.Writer;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import javax.naming.Context;
036:
037: import org.objectweb.carol.jndi.ns.NameServiceException;
038: import org.objectweb.carol.jndi.ns.NameServiceManager;
039: import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore;
040: import org.objectweb.carol.util.configuration.ConfigurationException;
041: import org.objectweb.carol.util.configuration.ConfigurationRepository;
042: import org.ow2.easybeans.component.api.EZBComponentException;
043: import org.ow2.easybeans.component.itf.RegistryComponent;
044: import org.ow2.util.log.Log;
045: import org.ow2.util.log.LogFactory;
046:
047: /**
048: * This component allows to start a registry.
049: * @author Florent Benoit
050: */
051: public class CarolComponent implements RegistryComponent {
052:
053: /**
054: * Logger.
055: */
056: private Log logger = LogFactory.getLog(CarolComponent.class);
057:
058: /**
059: * Default prefix protocol.
060: */
061: private static final String DEFAULT_PREFIX_PROTOCOL = "rmi";
062:
063: /**
064: * List of protocols.
065: */
066: private List<Protocol> protocols = null;
067:
068: /**
069: * Global Initial Context Factory.
070: */
071: private String initialContextFactory = null;
072:
073: /**
074: * Creates a new Carol component.
075: */
076: public CarolComponent() {
077: protocols = new ArrayList<Protocol>();
078: }
079:
080: /**
081: * Init method.<br/>
082: * This method is called before the start method.
083: * @throws EZBComponentException if the initialization has failed.
084: */
085: public void init() throws EZBComponentException {
086: // check
087: if (protocols == null || protocols.isEmpty()) {
088: logger
089: .debug("No protocols, use the existing carol configuration");
090: return;
091: }
092:
093: // Get Value of the Initial Factory
094: initialContextFactory = System
095: .getProperty(Context.INITIAL_CONTEXT_FACTORY);
096:
097: // List of protocols
098: String lstProtocol = null;
099:
100: // Create configuration
101: StringBuilder carolConf = new StringBuilder();
102: for (Protocol protocol : protocols) {
103: // Build list of protocols
104: if (lstProtocol != null) {
105: lstProtocol += ",";
106: lstProtocol += protocol.getName();
107: } else {
108: lstProtocol = protocol.getName();
109: }
110:
111: //Define URL
112: carolConf.append("carol.");
113: carolConf.append(protocol.getName());
114: carolConf.append(".url=");
115: if (protocol.getUrl() != null) {
116: // set defined URL
117: carolConf.append(protocol.getUrl());
118: } else {
119: // compute URL
120: String host = protocol.getHostname();
121: int portNumber = protocol.getPortNumber();
122: carolConf.append(DEFAULT_PREFIX_PROTOCOL);
123: carolConf.append("://");
124: carolConf.append(host);
125: carolConf.append(":");
126: carolConf.append(portNumber);
127: }
128: carolConf.append("\n");
129:
130: }
131:
132: // Set carol mode in server mode (will use fixed port if set)
133: System.setProperty("carol.server.mode", "true");
134: try {
135: // hack
136: Writer fw;
137: File fConf = new File(System.getProperty("java.io.tmpdir")
138: + File.separator + System.getProperty("user.name")
139: + "ejb3-carol.properties");
140: try {
141: fw = new FileWriter(fConf);
142: fw.write("carol.protocols=");
143: fw.write(lstProtocol);
144: fw.write("\n");
145: fw.write(carolConf.toString());
146: fw.write("carol.jvm.rmi.local.registry=true");
147: fw.close();
148: } catch (IOException e) {
149: e.printStackTrace();
150: }
151: ConfigurationRepository.init(fileToURL(fConf));
152: } catch (ConfigurationException e) {
153: throw new EZBComponentException(
154: "Cannot initialize registry", e);
155: }
156:
157: try {
158: ConfigurationRepository
159: .addInterceptors("jrmp",
160: "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer");
161: } catch (ConfigurationException e) {
162: throw new EZBComponentException(
163: "Cannot add JOTM interceptors", e);
164: }
165:
166: try {
167: ConfigurationRepository.addInterceptors("iiop",
168: "org.objectweb.jotm.ots.OTSORBInitializer");
169: } catch (ConfigurationException e) {
170: throw new EZBComponentException(
171: "Cannot add JOTM interceptors", e);
172: }
173:
174: try {
175: ConfigurationRepository
176: .addInterceptors(
177: "jrmp",
178: "org.ow2.easybeans.security.propagation.rmi.jrmp.interceptors.SecurityInitializer");
179: } catch (ConfigurationException e) {
180: throw new EZBComponentException(
181: "Cannot add Security interceptors", e);
182: }
183:
184: // Init Initializers
185: JInterceptorStore.getJRMPInitializers();
186:
187: }
188:
189: /**
190: * Start method.<br/>
191: * This method is called after the init method.
192: * @throws EZBComponentException if the start has failed.
193: */
194: public void start() throws EZBComponentException {
195: // Start registry only if there are protocols
196: if (protocols != null && !protocols.isEmpty()) {
197: try {
198: NameServiceManager.startNS();
199: } catch (NameServiceException e) {
200: throw new EZBComponentException(
201: "Cannot start registry", e);
202: }
203: }
204: }
205:
206: /**
207: * Stop method.<br/>
208: * This method is called when component needs to be stopped.
209: * @throws EZBComponentException if the stop is failing.
210: */
211: public void stop() throws EZBComponentException {
212: // Stop registry only if there are protocols
213: if (protocols != null && !protocols.isEmpty()) {
214: try {
215: NameServiceManager.stopNS();
216: } catch (NameServiceException e) {
217: throw new EZBComponentException(
218: "Cannot stop the registry", e);
219: }
220:
221: // Restore previous value of the Initial Context Factory
222: if (initialContextFactory != null) {
223: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
224: initialContextFactory);
225: } else {
226: // Remove property
227: System.getProperties().remove(
228: Context.INITIAL_CONTEXT_FACTORY);
229: }
230:
231: }
232: }
233:
234: /**
235: * Gets the protocols defined for the start.
236: * @return the list of protocols.
237: */
238: public List<Protocol> getProtocols() {
239: return protocols;
240: }
241:
242: /**
243: * Sets the list of protocols.
244: * @param protocols the list of protocols configured for this server.
245: */
246: public void setProtocols(final List<Protocol> protocols) {
247: this .protocols = protocols;
248: }
249:
250: /**
251: * Gets the default Provider URL.
252: * @return the provider URL that is used by default.
253: */
254: public String getProviderURL() {
255: return ConfigurationRepository.getCurrentConfiguration()
256: .getProviderURL();
257: }
258:
259: }
|