1 2 package SOFA.SOFAnode.Run.Deployment; 3 4 import java.net.InetAddress ; 5 import java.net.UnknownHostException ; 6 7 import org.omg.CosNaming.NameComponent ; 8 9 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistry; 10 import SOFA.SOFAnode.Run.DeplDockRegistry.DeplDockRegistryHelper; 11 12 13 17 public class DeplDockImpl implements DeplDock { 18 19 protected TechnologyDescriptor[] description; 21 22 23 protected String name; 24 25 protected String node; 26 27 protected DeplDockRegistry ddr; 28 29 30 protected java.util.Hashtable components; 31 32 33 protected java.util.Hashtable sroles; 34 35 36 protected java.util.Hashtable applications; 37 38 43 public DeplDockImpl(String _name, String _node, DeplDockRegistry _ddr) { 44 name = _name; 45 node = _node; 46 ddr = _ddr; 47 components = new java.util.Hashtable (); 48 sroles = new java.util.Hashtable (); 49 applications = new java.util.Hashtable (); 50 defineDescription(); 51 } 52 53 58 protected void defineDescription() { 59 description = new TechnologyDescriptor [3]; 60 63 } 64 65 69 70 public TechnologyDescriptor[] describeUnderlyingEnvironment() { 71 return description; 72 } 73 74 public SOFA.Connector.Reference instantiate(DeploymentDescriptor dd, String name) throws DeploymentException { 75 String id = ddr.getUniqueID(); 76 ((DeploymentDescriptorImpl) dd).setInstanceId(id); 77 if (name == null || name.compareTo("")==0) 78 name = dd.getInstanceName(); 79 String cid = name+":"+id; 80 SOFA.Component.DCUP.DCUPComponentManager cm; 81 try { 82 cm = new SOFA.Component.DCUP.DCUPComponentManagerImpl(dd, node, name, this, id); 83 } catch (SOFA.Component.ComponentLifecycleException e) { 84 throw new DeploymentException("ComponentLifecycleException: "+e.getMessage(), e); 85 } 86 SOFA.Connector.RoleBase srole; 87 92 srole = ((SOFA.Component.DCUP.DCUPComponentManagerImpl) cm).connToThisCM(); 93 94 components.put(cid, cm); 95 sroles.put(cid, srole); 96 if (dd.isSystem()) 97 applications.put(cid, cm); 98 99 101 return srole.getSOFAReference(); 102 103 } 104 105 106 public String [] namesOfAllRunningComponents() { 107 String [] ret = new String [components.size()]; 108 int i = 0; 109 for (java.util.Enumeration e = components.keys() ; e.hasMoreElements() ; i++) { 110 ret[i] = new String ((String ) e.nextElement()); 111 } 112 return ret; 113 } 114 115 public String [] namesOfAllRunningApplications() { 116 String [] ret = new String [applications.size()]; 117 int i = 0; 118 for (java.util.Enumeration e = applications.keys() ; e.hasMoreElements() ; i++) { 119 ret[i] = new String ((String ) e.nextElement()); 120 } 121 return ret; 122 } 123 124 125 126 130 public static SOFA.Connector.Reference removeLocalProfile(SOFA.Connector.Reference ref) { 131 boolean b = false; 132 for(int i=0; i<ref.profiles.length; i++) { 133 if (ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.LocalProfile) { 134 b = true; 135 } 136 } 137 if (b) { 138 SOFA.Connector.TaggedProfile[] p = new SOFA.Connector.TaggedProfile [ref.profiles.length]; 139 int j = 0; 140 for (int i=0; i<ref.profiles.length; i++) { 141 if (!(ref.profiles[i] instanceof SOFA.Connector.ECG.Profiles.LocalProfile)) { 142 p[j] = ref.profiles[i]; 143 j++; 144 } 145 } 146 ref.profiles = p; 147 } 148 return ref; 149 } 150 151 155 public static byte[] sofaReferenceToBytes(SOFA.Connector.Reference ref) { 156 try { 157 cz.cuni.sofa.lib.Impl.ByteArrayOutputStream os = new cz.cuni.sofa.lib.Impl.ByteArrayOutputStream(); 159 ref._write(os); 161 return os.toByteArray(); 162 } catch (java.io.IOException e) { 163 return null; 164 } 165 } 166 167 synchronized public void removeComponent(String name) throws SOFA.SOFAnode.Run.Deployment.DeploymentException { 168 if (!sroles.containsKey(name)) 169 throw new DeploymentException("No component with given name"); 170 SOFA.Component.DCUP.DCUPComponentManagerImpl cm = (SOFA.Component.DCUP.DCUPComponentManagerImpl) components.get(name); 171 if (cm == null) { 172 sroles.remove(name); 173 throw new DeploymentException("Error in the component registering"); 174 } 175 if (!cm.isStopped()) 176 throw new DeploymentException("Component can't be removed"); 177 sroles.remove(name); 178 components.remove(name); 179 if (applications.containsKey(name)) 180 applications.remove(name); 181 } 182 183 187 public static SOFA.Connector.Reference bytesToSofaReference(byte[] ref) { 188 try { 189 cz.cuni.sofa.lib.Impl.ByteArrayInputStream is = new cz.cuni.sofa.lib.Impl.ByteArrayInputStream (ref); 190 SOFA.Connector.Reference ret = new SOFA.Connector.Reference (); 191 ret._read(is); 192 return ret; 193 } catch (java.io.IOException e) { 194 return null; 195 } 196 } 197 198 synchronized public SOFA.Connector.Reference getReference(String name) throws SOFA.SOFAnode.Run.Deployment.DeploymentException { 199 if (!sroles.containsKey(name)) 200 throw new DeploymentException("No component with given name"); 201 return ((SOFA.Connector.RoleBase) sroles.get(name)).getSOFAReference(); 202 } 203 204 205 212 public static void main(String [] argv) { 213 if (argv.length != 1) { 214 System.out.println("Set name of the dock"); 215 System.exit(1); 216 } 217 218 if (System.getSecurityManager() == null) { 219 System.setSecurityManager(new java.rmi.RMISecurityManager ()); 220 } 221 222 String sofaNodeName = System.getProperty("sofa.nodename",""); 223 if (sofaNodeName.compareTo("")==0) { 224 try { 225 sofaNodeName = InetAddress.getLocalHost().getHostName(); 226 } catch (UnknownHostException e) { 227 System.out.println("Can't get host name. Set name to property \"sofa.nodename\""); 228 System.exit(1); 229 } 230 } 231 232 System.setProperty("sofa.nodename",sofaNodeName); 233 234 SOFA.Util.CORBAAccess ca = new SOFA.Util.CORBAAccess(); 235 236 NameComponent nc = new NameComponent ("DeplDockRegistry", ""); 237 NameComponent path[] = {nc}; 238 DeplDockRegistry ddr = null; 239 try { 240 ddr = DeplDockRegistryHelper.narrow(SOFA.Util.CORBAAccess.ncRef.resolve(path)); 241 } catch (Exception e) { 242 System.out.println("Can't get DeplDock registry."); 243 e.printStackTrace(); 244 System.exit(1); 245 } 246 247 DeplDockImpl ddi = new DeplDockImpl(argv[0], sofaNodeName, ddr); 248 SOFA.Connector.RoleBase srole = null; 249 try { 250 srole = SOFA.Connector.Boot.DeplDockConnector.createSrv((DeplDock) ddi, argv[0]); 251 } catch (SOFA.Connector.ConnectorException e) { 252 System.out.println("Can't create connector to depldock"); 253 e.printStackTrace(); 254 System.exit(1); 255 } 256 SOFA.Connector.Reference ref = srole.getSOFAReference(); 257 byte[] strRef = sofaReferenceToBytes(ref); 259 ddr.register(strRef, argv[0]); 261 System.out.println("DeplDock "+argv[0]+" is registered and running."); 262 263 ca.run(); 264 } 265 } 266 | Popular Tags |