1 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 ; 31 import java.io.FileWriter ; 32 import java.io.IOException ; 33 import java.io.Writer ; 34 import java.util.ArrayList ; 35 import java.util.List ; 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 50 public class CarolComponent implements RegistryComponent { 51 52 55 private JLog logger = JLogFactory.getLog(CarolComponent.class); 56 57 60 private static final String DEFAULT_PREFIX_PROTOCOL = "rmi"; 61 62 65 private List <Protocol> protocols = null; 66 67 70 public CarolComponent() { 71 protocols = new ArrayList <Protocol>(); 72 } 73 74 79 public void init() throws EZBComponentException { 80 if (protocols == null || protocols.isEmpty()) { 82 logger.info("No protocols, use the existing carol configuration"); 83 return; 84 } 85 86 String lstProtocol = null; 88 89 StringBuilder carolConf = new StringBuilder (); 91 for (Protocol protocol : protocols) { 92 if (lstProtocol != null) { 94 lstProtocol += ","; 95 lstProtocol += protocol.getName(); 96 } else { 97 lstProtocol = protocol.getName(); 98 } 99 100 101 carolConf.append("carol."); 103 carolConf.append(protocol.getName()); 104 carolConf.append(".url="); 105 if (protocol.getUrl() != null) { 106 carolConf.append(protocol.getUrl()); 108 } else { 109 String 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 System.setProperty("carol.server.mode", "true"); 124 try { 125 Writer fw; 127 File fConf = new File (System.getProperty("java.io.tmpdir") + File.separator + System.getProperty("user.name") + "ejb3-carol.properties"); 128 try { 129 fw = new FileWriter (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 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 165 public void start() throws EZBComponentException { 166 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 182 public void stop() throws EZBComponentException { 183 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 197 public List <Protocol> getProtocols() { 198 return protocols; 199 } 200 201 205 public void setProtocols(final List <Protocol> protocols) { 206 this.protocols = protocols; 207 } 208 209 213 public String getProviderURL() { 214 return ConfigurationRepository.getCurrentConfiguration().getProviderURL(); 215 } 216 217 } 218 | Popular Tags |