| 1 |
|
//---PACKAGE DECLARATION--- |
| 2 |
< |
package uk.ac.ukc.iscream.refman; |
| 2 |
> |
package uk.org.iscream.componentmanager; |
| 3 |
|
|
| 4 |
|
//---IMPORTS--- |
| 5 |
|
import org.omg.CORBA.ORB; |
| 6 |
|
import org.omg.CosNaming.*; |
| 7 |
|
import org.omg.PortableServer.*; |
| 8 |
< |
import uk.ac.ukc.iscream.core.*; |
| 8 |
> |
import uk.org.iscream.core.*; |
| 9 |
> |
import uk.org.iscream.util.*; |
| 10 |
|
|
| 11 |
|
/** |
| 12 |
|
* This class returns references for global system objects. |
| 13 |
|
* This class is used to create and return references to objects |
| 14 |
|
* that are required throughout the system. Most of which |
| 15 |
< |
* are CORBA based. It also handles ORB initialisation. |
| 15 |
> |
* are CORBA based. It also manages the ORB for the component. |
| 16 |
|
* |
| 17 |
|
* It is a singleton object with a static method to obtain a |
| 18 |
|
* reference to it. This removes the need for passing |
| 34 |
|
//---STATIC METHODS--- |
| 35 |
|
|
| 36 |
|
/** |
| 37 |
< |
* This essentially formats the toString() source for an |
| 38 |
< |
* object wanting to make a log entry. |
| 37 |
> |
* This returns a reference to the single instance of the |
| 38 |
> |
* ReferenceManager. |
| 39 |
|
* |
| 40 |
< |
* @param source the source of the call |
| 41 |
< |
* @param rev the CVS revision number of the caller |
| 40 |
> |
* This creates the single instance of the ReferenceManager |
| 41 |
> |
* if it does not exist by calling the private constructor. |
| 42 |
|
* |
| 43 |
< |
* @return the formatted string |
| 44 |
< |
|
| 45 |
< |
public static String logString(Object source, String rev) { |
| 43 |
> |
* @return a reference to the ReferenceManager |
| 44 |
> |
*/ |
| 45 |
> |
public static ReferenceManager getInstance() { |
| 46 |
|
if (_instance == null) { |
| 47 |
< |
return "unamed{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; |
| 47 |
> |
_instance = new ReferenceManager(); |
| 48 |
|
} |
| 49 |
< |
return getInstance().getName() + "{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; |
| 49 |
> |
return _instance; |
| 50 |
|
} |
| 50 |
– |
` */ |
| 51 |
|
|
| 52 |
+ |
//---CONSTRUCTORS--- |
| 53 |
+ |
|
| 54 |
|
/** |
| 55 |
< |
* This creates the single instance of the ReferenceManager by |
| 56 |
< |
* calling the private constructor. If it is called twice, |
| 57 |
< |
* it detects an instance already exits and throws an exception. |
| 55 |
> |
* This is a private constructor |
| 56 |
> |
* This ensures that the system performs according to a Singleton design pattern |
| 57 |
> |
*/ |
| 58 |
> |
private ReferenceManager() { |
| 59 |
> |
_orb = ORB.init(new String[] {}, null); |
| 60 |
> |
} |
| 61 |
> |
|
| 62 |
> |
//---PUBLIC METHODS--- |
| 63 |
> |
|
| 64 |
> |
/** |
| 65 |
> |
* Obtains a CORBA reference through the naming service |
| 66 |
> |
* for the named object. |
| 67 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 68 |
|
* |
| 69 |
< |
* @param args the args to be passed to the ORB |
| 58 |
< |
* @param name the name of the component that will use this singleton |
| 69 |
> |
* @param name the name of the CORBA object to resolve |
| 70 |
|
* |
| 71 |
< |
* @return a reference to the ReferenceManager |
| 71 |
> |
* @return a reference to the object |
| 72 |
> |
*/ |
| 73 |
> |
public org.omg.CORBA.Object getCORBARef(String name) { |
| 74 |
> |
org.omg.CORBA.Object objRef = null; |
| 75 |
> |
try { |
| 76 |
> |
objRef = getNS().resolve(getNS().to_name(name)); |
| 77 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
| 78 |
> |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
| 79 |
> |
" Please check with your CORBA naming service provider."); |
| 80 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
| 81 |
> |
dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
| 82 |
> |
" Please check with your CORBA naming service provider."); |
| 83 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
| 84 |
> |
recoverWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
| 85 |
> |
" Please check that this component is running."); |
| 86 |
> |
} |
| 87 |
> |
return objRef; |
| 88 |
> |
} |
| 89 |
> |
|
| 90 |
> |
/** |
| 91 |
> |
* Binds a given servant with the given name |
| 92 |
> |
* to the naming service. |
| 93 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 94 |
|
* |
| 95 |
< |
* @throws AlreadyInitialisedException if this method has already been called |
| 95 |
> |
* @param objRef a reverence to the servant object |
| 96 |
> |
* @param name the name to bind to the naming service with |
| 97 |
|
*/ |
| 98 |
< |
public static ReferenceManager init(String[] args, String name) throws AlreadyInitialisedException { |
| 99 |
< |
if (_instance != null) { |
| 100 |
< |
throw new AlreadyInitialisedException("init has already been called"); |
| 98 |
> |
public void bindToOrb(org.omg.PortableServer.Servant objRef, String name) { |
| 99 |
> |
try { |
| 100 |
> |
getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); |
| 101 |
> |
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { |
| 102 |
> |
dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
| 103 |
> |
" This indicates an error with your ORB."); |
| 104 |
> |
} catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { |
| 105 |
> |
dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
| 106 |
> |
" This indicates an error with your ORB."); |
| 107 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
| 108 |
> |
dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
| 109 |
> |
" Please check with your CORBA naming service provider."); |
| 110 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
| 111 |
> |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
| 112 |
> |
" Please check with your CORBA naming service provider."); |
| 113 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
| 114 |
> |
recoverWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
| 115 |
> |
" Please check with your CORBA naming service provider."); |
| 116 |
> |
} catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { |
| 117 |
> |
dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
| 118 |
> |
" Another component with this name is already running."); |
| 119 |
|
} |
| 68 |
– |
_instance = new ReferenceManager(args, name); |
| 69 |
– |
return _instance; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
/** |
| 123 |
< |
* This returns a reference to the single instance of the |
| 124 |
< |
* ReferenceManager. |
| 123 |
> |
* Activates the POA |
| 124 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 125 |
> |
*/ |
| 126 |
> |
public void activatePOA() { |
| 127 |
> |
try { |
| 128 |
> |
getRootPOA().the_POAManager().activate(); |
| 129 |
> |
} catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { |
| 130 |
> |
dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
| 131 |
> |
" This indicates an error with your ORB."); |
| 132 |
> |
} |
| 133 |
> |
} |
| 134 |
> |
|
| 135 |
> |
/** |
| 136 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 137 |
> |
* method to provide clean logging (every class should have this). |
| 138 |
|
* |
| 139 |
< |
* @return a reference to the ReferenceManager |
| 139 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
| 140 |
> |
* to format the toString() |
| 141 |
|
* |
| 142 |
< |
* @throws NotInitialisedException if the reference manager has not been initialised |
| 142 |
> |
* @return the name of this class and its CVS revision |
| 143 |
|
*/ |
| 144 |
< |
public static ReferenceManager getInstance() throws NotInitialisedException { |
| 145 |
< |
if (_instance == null) { |
| 146 |
< |
throw new NotInitialisedException("attempt to obtain reference when not initialised"); |
| 147 |
< |
} |
| 148 |
< |
return _instance; |
| 144 |
> |
public String toString() { |
| 145 |
> |
return FormatName.getName( |
| 146 |
> |
_name, |
| 147 |
> |
getClass().getName(), |
| 148 |
> |
REVISION); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
< |
//---CONSTRUCTORS--- |
| 151 |
> |
//---PRIVATE METHODS--- |
| 152 |
|
|
| 153 |
|
/** |
| 154 |
< |
* This is a private constructor |
| 155 |
< |
* This ensures that the system performs according to a Singleton design pattern |
| 154 |
> |
* If there are any CORBA errors this method is called with the |
| 155 |
> |
* error message. |
| 156 |
|
* |
| 157 |
< |
* @param args the args to be passed to the ORB |
| 158 |
< |
* @param name the name of the component that will use this singleton |
| 157 |
> |
* The exception this throws is generic to CORBA communication |
| 158 |
> |
* problems, this is a Runtime exception and is typically only |
| 159 |
> |
* explicitly caught in the ComponentManager for the current VM |
| 160 |
> |
* The ComponentManager can then decide what course of action to |
| 161 |
> |
* take. |
| 162 |
> |
* |
| 163 |
> |
* Also, if it can, it will log the message with the logger. |
| 164 |
> |
* |
| 165 |
> |
* @param message the error message to die with |
| 166 |
|
*/ |
| 167 |
< |
private ReferenceManager(String[] args, String name) { |
| 168 |
< |
_orb = ORB.init(args, null); |
| 169 |
< |
_name = name; |
| 170 |
< |
getLogger().write(toString(), Logger.SYSINIT, "created"); |
| 167 |
> |
private void recoverWithError(String message) throws ComponentCORBAException { |
| 168 |
> |
if (_logger != null) { |
| 169 |
> |
_logger.write(toString(), Logger.WARNING, "component CORBA error - " + message); |
| 170 |
> |
} |
| 171 |
> |
throw new ComponentCORBAException(message); |
| 172 |
|
} |
| 173 |
|
|
| 102 |
– |
//---PUBLIC METHODS--- |
| 174 |
|
|
| 175 |
|
/** |
| 176 |
< |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 177 |
< |
* method to provide clean logging (every class should have this). |
| 176 |
> |
* If there are any CORBA errors this method is called with the |
| 177 |
> |
* error message. |
| 178 |
|
* |
| 179 |
< |
* @return the name of this class and its CVS revision |
| 179 |
> |
* Currently this prints the error, on the local err stream. Will |
| 180 |
> |
* print to the logger if one is available. |
| 181 |
> |
* |
| 182 |
> |
* ANY CALLS TO THIS METHOD CAUSE |
| 183 |
> |
* THE SYSTEM TO EXIT WITH A NON |
| 184 |
> |
* ZERO CODE! |
| 185 |
> |
* |
| 186 |
> |
* @param message the error message to die with |
| 187 |
|
*/ |
| 188 |
< |
public String toString() { |
| 189 |
< |
return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 188 |
> |
private void dieWithError(String message) { |
| 189 |
> |
System.err.println(message); |
| 190 |
> |
if (_logger != null) { |
| 191 |
> |
_logger.write(toString(), Logger.FATAL, message); |
| 192 |
> |
} |
| 193 |
> |
System.exit(1); |
| 194 |
|
} |
| 195 |
|
|
| 114 |
– |
//---PRIVATE METHODS--- |
| 196 |
|
|
| 197 |
+ |
|
| 198 |
|
//---ACCESSOR/MUTATOR METHODS--- |
| 199 |
|
|
| 200 |
|
/** |
| 208 |
|
|
| 209 |
|
/** |
| 210 |
|
* Returns a reference to the Root POA |
| 211 |
< |
* This will throw a RuntimeException if there is an error |
| 211 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 212 |
|
* |
| 213 |
|
* @return the reference this class holds |
| 214 |
|
*/ |
| 218 |
|
try { |
| 219 |
|
objRef = getORB().resolve_initial_references("RootPOA"); |
| 220 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
| 221 |
< |
throw new RuntimeException("CRITICAL:Unable to resolve reference to the RootPOA!\n" + |
| 222 |
< |
" This indicates an error with your ORB."); |
| 221 |
> |
dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
| 222 |
> |
" This indicates an error with your ORB."); |
| 223 |
|
} |
| 224 |
|
_rootPOA = POAHelper.narrow(objRef); |
| 225 |
|
} |
| 228 |
|
|
| 229 |
|
/** |
| 230 |
|
* Returns a reference to the Naming Service |
| 231 |
< |
* This will throw a RuntimeException if there is an error |
| 231 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 232 |
|
* |
| 233 |
|
* @return the reference this class holds |
| 234 |
|
*/ |
| 238 |
|
try { |
| 239 |
|
objRef = getORB().resolve_initial_references("NameService"); |
| 240 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
| 241 |
< |
throw new RuntimeException("CRITICAL:Unable to resolve reference to the Naming Service!\n" + |
| 242 |
< |
" Please check with your CORBA naming service provider."); |
| 241 |
> |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
| 242 |
> |
" Please check with your CORBA naming service provider."); |
| 243 |
|
} |
| 244 |
|
_ns = NamingContextExtHelper.narrow(objRef); |
| 245 |
|
} |
| 246 |
+ |
// check we managed to talk to the naming service |
| 247 |
+ |
if (_ns == null) { |
| 248 |
+ |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
| 249 |
+ |
" Please check with your CORBA naming service provider."); |
| 250 |
+ |
} |
| 251 |
|
return _ns; |
| 252 |
|
} |
| 253 |
|
|
| 254 |
|
/** |
| 255 |
|
* Returns a reference to the configuration manager |
| 256 |
< |
* This will throw a RuntimeException if there is an error |
| 256 |
> |
* Calls the dieWithError() if any exceptions happen! |
| 257 |
|
* |
| 258 |
|
* @return the reference this class holds |
| 259 |
|
*/ |
| 260 |
|
public ConfigurationManager getCM() { |
| 261 |
|
if (_cm == null) { |
| 262 |
< |
org.omg.CORBA.Object objRef = null; |
| 176 |
< |
try { |
| 177 |
< |
objRef = getNS().resolve(getNS().to_name("iscream.ConfigurationManager")); |
| 178 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
| 179 |
< |
throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.ConfigurationManager!\n" + |
| 180 |
< |
" This indicates an error with your ORB."); |
| 181 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
| 182 |
< |
throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.ConfigurationManager!\n" + |
| 183 |
< |
" This indicates an error with your ORB."); |
| 184 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
| 185 |
< |
throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.ConfigurationManager!\n" + |
| 186 |
< |
" Please check that this component is running."); |
| 187 |
< |
} |
| 188 |
< |
_cm = ConfigurationManagerHelper.narrow(objRef); |
| 262 |
> |
_cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); |
| 263 |
|
} |
| 264 |
+ |
// check we managed to talk to the configuration manager |
| 265 |
+ |
if (_cm == null) { |
| 266 |
+ |
dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + |
| 267 |
+ |
" Please check with your CORBA naming service provider."); |
| 268 |
+ |
} |
| 269 |
|
return _cm; |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
/** |
| 273 |
|
* Returns a reference to the logger |
| 274 |
< |
* This will throw a RuntimeException if there is an error |
| 274 |
> |
* This will throw a Error if there is an error |
| 275 |
|
* |
| 276 |
|
* @return the reference this class holds |
| 277 |
|
*/ |
| 278 |
|
public Logger getLogger() { |
| 279 |
|
if (_logger == null) { |
| 280 |
< |
org.omg.CORBA.Object objRef = null; |
| 202 |
< |
try { |
| 203 |
< |
objRef = getNS().resolve(getNS().to_name("iscream.Logger")); |
| 204 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
| 205 |
< |
throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.Logger!\n" + |
| 206 |
< |
" This indicates an error with your ORB."); |
| 207 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
| 208 |
< |
throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.Logger!\n" + |
| 209 |
< |
" This indicates an error with your ORB."); |
| 210 |
< |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
| 211 |
< |
throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.Logger!\n" + |
| 212 |
< |
" Please check that this component is running."); |
| 213 |
< |
} |
| 214 |
< |
_logger = LoggerHelper.narrow(objRef); |
| 280 |
> |
_logger = LoggerHelper.narrow(getCORBARef("iscream.Logger")); |
| 281 |
|
} |
| 282 |
|
return _logger; |
| 283 |
|
} |
| 284 |
|
|
| 219 |
– |
/** |
| 220 |
– |
* Returns a reference to the name |
| 221 |
– |
* |
| 222 |
– |
* @return the reference this class holds |
| 223 |
– |
*/ |
| 224 |
– |
public String getName() { |
| 225 |
– |
return _name; |
| 226 |
– |
} |
| 227 |
– |
|
| 285 |
|
//---ATTRIBUTES--- |
| 286 |
|
|
| 287 |
|
/** |
| 310 |
|
private Logger _logger; |
| 311 |
|
|
| 312 |
|
/** |
| 313 |
< |
* The name |
| 313 |
> |
* This is the friendly identifier of the |
| 314 |
> |
* component this class is running in. |
| 315 |
> |
* eg, a Filter may be called "filter1", |
| 316 |
> |
* If this class does not have an owning |
| 317 |
> |
* component, a name from the configuration |
| 318 |
> |
* can be placed here. This name could also |
| 319 |
> |
* be changed to null for utility classes. |
| 320 |
|
*/ |
| 321 |
< |
private String _name; |
| 321 |
> |
private String _name = null; |
| 322 |
|
|
| 323 |
|
//---STATIC ATTRIBUTES--- |
| 324 |
|
|