KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > configuration > Protocol


1 /**
2  * Copyright (C) 2005 - Bull S.A.
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
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: Protocol.java,v 1.1 2005/04/07 15:07:07 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.carol.util.configuration;
26
27 import java.util.Properties JavaDoc;
28
29 import javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc;
30
31
32 /**
33  * This class defines commons attributes of a protocol for Carol.<br>
34  * An rmi configuration relies on a protocol by specifying properties. For
35  * example a protocol is composed of a Prodelegate Implementation class, a
36  * registry class, etc.<br>
37  * But the PROVIDER_URL could be different. This is done in Configuration
38  * object. (one protocol could be associated to different configurations)<br>
39  * ie : JRMP --> jrmp1 with localhost:1099, jrmp2 with localhost:1100
40  * @author Florent Benoit
41  */

42 public class Protocol {
43
44     /**
45      * Name of this protocol
46      */

47     private String JavaDoc name = null;
48
49     /**
50      * Properties of this protocol
51      */

52     private Properties JavaDoc properties = null;
53
54     /**
55      * PortableRemoteObject object
56      */

57     private PortableRemoteObjectDelegate JavaDoc portableRemoteObjectDelegate = null;
58
59     /**
60      * Name of the class of PortableRemoteObject
61      */

62     private String JavaDoc portableRemoteObjectClassName = null;
63
64     /**
65      * Context.INITIAL_CONTEXT_FACTORY class implementation
66      */

67     private String JavaDoc initialContextFactoryClassName = null;
68
69     /**
70      * Registry class (implementing org.objectweb.carol.jndi.ns.NameService
71      * class)
72      */

73     private String JavaDoc registryClassName = null;
74
75     /**
76      * Prefix for interceptors
77      */

78     private String JavaDoc interceptorNamePrefix = null;
79
80     /**
81      * Gets value of properties object
82      * @param key the key of the properties
83      * @return value stored in a property object
84      * @throws ConfigurationException if properties are missing
85      */

86     protected String JavaDoc getValue(String JavaDoc key) throws ConfigurationException {
87         // properties cannot be null, check in constructor
88
String JavaDoc s = properties.getProperty(key);
89         if (s == null) {
90             throw new ConfigurationException("Property '" + key + "' was not found in the properties object of the protocol, properties are :'" + properties + "'");
91         }
92         return s;
93     }
94
95
96     /**
97      * Build a new protocol object with given parameters
98      * @param name the name of this protocol
99      * @param properties properties of this protocol
100      * @throws ConfigurationException if properties are missing
101      */

102     public Protocol(String JavaDoc name, Properties JavaDoc properties) throws ConfigurationException {
103         if (name == null || "".equals(name)) {
104             throw new ConfigurationException("Cannot build a protocol with null or empty name");
105         }
106         this.name = name;
107
108         if (properties == null) {
109             throw new ConfigurationException("Cannot build a new protocol without properties");
110         }
111         this.properties = properties;
112
113         String JavaDoc prefixProtocol = CarolDefaultValues.CAROL_PREFIX + "." + name + ".";
114
115         // PRODelegate
116
portableRemoteObjectClassName = getValue(prefixProtocol + CarolDefaultValues.PRO_PREFIX);
117
118         // Initial Context factory
119
this.initialContextFactoryClassName = getValue(prefixProtocol + CarolDefaultValues.FACTORY_PREFIX);
120
121         // Registry class
122
this.registryClassName = getValue(prefixProtocol + CarolDefaultValues.NS_PREFIX);
123
124
125         // JVM interceptors
126
interceptorNamePrefix = properties.getProperty(CarolDefaultValues.CAROL_PREFIX + "." + name + "."
127                 + CarolDefaultValues.INTERCEPTOR_PKGS_PREFIX);
128
129         String JavaDoc interceptorValues = properties.getProperty(CarolDefaultValues.CAROL_PREFIX + "." + name + "."
130                 + CarolDefaultValues.INTERCEPTOR_VALUES_PREFIX);
131
132         // set the jvm interceptors flag
133
if ((interceptorNamePrefix != null) && (interceptorValues != null)) {
134             //Parse jvm the properties
135
String JavaDoc[] values = interceptorValues.split(",");
136             for (int s = 0; s < values.length; s++) {
137                 String JavaDoc value = values[s];
138                 addInterceptor(value);
139             }
140         }
141
142
143     }
144
145     /**
146      * Add an interceptor for the given protocol
147      * @param interceptorInitializer the class of the interceptor initializer
148      */

149     public void addInterceptor(String JavaDoc interceptorInitializer) {
150         System.setProperty(interceptorNamePrefix + "." + interceptorInitializer, "");
151         if (TraceCarol.isDebugCarol()) {
152             TraceCarol.debugCarol("Setting interceptor " + interceptorNamePrefix + "." + interceptorInitializer + "/");
153         }
154     }
155
156
157     /**
158      * @return the initialContextFactory ClassName.
159      */

160     public String JavaDoc getInitialContextFactoryClassName() {
161         return initialContextFactoryClassName;
162     }
163
164     /**
165      * @return the registry ClassName.
166      */

167     public String JavaDoc getRegistryClassName() {
168         return registryClassName;
169     }
170
171
172     /**
173      * @return the portableRemoteObject delegate.
174      */

175     public PortableRemoteObjectDelegate JavaDoc getPortableRemoteObject() {
176         if (portableRemoteObjectDelegate != null) {
177             return portableRemoteObjectDelegate;
178         }
179         try {
180             Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(portableRemoteObjectClassName);
181             portableRemoteObjectDelegate = (PortableRemoteObjectDelegate JavaDoc) clazz.newInstance();
182         } catch (Exception JavaDoc e) {
183             IllegalStateException JavaDoc newEx = new IllegalStateException JavaDoc("Cannot build PortableRemoteObjectDelegate class '" + portableRemoteObjectClassName + "'");
184             newEx.initCause(e);
185             throw newEx;
186         }
187
188         return portableRemoteObjectDelegate;
189     }
190
191
192     /**
193      * @return the name of this protocol.
194      */

195     public String JavaDoc getName() {
196         return name;
197     }
198
199
200 }
201
Popular Tags