1 26 package org.objectweb.jonas.jaxr; 27 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 import java.util.Vector ; 33 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 38 import org.objectweb.jonas.common.JProp; 39 import org.objectweb.jonas.common.Log; 40 import org.objectweb.jonas.common.PropDump; 41 import org.objectweb.jonas.service.AbsServiceImpl; 42 import org.objectweb.jonas.service.ServiceException; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 47 48 52 public class JAXRServiceImpl extends AbsServiceImpl implements JAXRService { 53 54 57 private Context ictx = null; 58 61 private static Logger logger = null; 62 63 66 private Vector factoryNames = new Vector (); 67 68 71 private Map bindedFactories = new Hashtable (); 72 75 public static final String FACTORIES = "jonas.service.jaxr.factories"; 76 77 80 public static final String CLASS = "jonas.service.jaxr.class"; 81 82 85 private static final String JNDI_NAME = "jaxr.jndi.name"; 86 87 90 protected void doInit(Context ctx) throws ServiceException { 91 logger = Log.getLogger(Log.JONAS_JAXR_PREFIX); 93 94 try { 96 ictx = new InitialContext (); 97 } catch (NamingException e) { 98 logger.log(BasicLevel.ERROR, "Cannot create initial context during the jaxr service initializing"); 99 throw new ServiceException("Cannot create initial context during the jaxr service initializing", e); 100 } 101 102 String factories = null; 104 try { 105 factories = (String ) ctx.lookup(FACTORIES); 106 } catch (NamingException e) { 107 ; } 109 if (factories != null) { 110 String [] names = factories.split(", "); 111 for (int i = 0; i < names.length; i++) { 112 factoryNames.add(names[i].trim()); 113 } 114 } 115 if (logger.isLoggable(BasicLevel.DEBUG)) { 116 logger.log(BasicLevel.DEBUG, "jaxr service initialized"); 117 } 118 119 } 120 121 124 protected void doStart() throws ServiceException { 125 String factoryName = null; 127 for (Iterator i = factoryNames.iterator(); i.hasNext();) { 128 factoryName = (String ) i.next(); 129 try { 130 JProp prop = JProp.getInstance(factoryName); 131 if (logger.isLoggable(BasicLevel.DEBUG)) { 132 logger.log(BasicLevel.DEBUG, "Creating JAXR Connection Factory " + factoryName); 133 } 134 createJAXRConnection(prop.getConfigFileEnv()); 135 } catch (Exception e) { 136 if (logger.isLoggable(BasicLevel.ERROR)) { 137 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create jaxr factory " + factoryName + " : " + e); 138 logger.log(BasicLevel.ERROR, "Please check the " + factoryName + ".properties file"); 139 } 140 } 141 } 142 } 143 144 147 protected void doStop() throws ServiceException { 148 removeAllJAXRConnections(); 149 if (logger.isLoggable(BasicLevel.DEBUG)) { 150 logger.log(BasicLevel.DEBUG, "jaxr service stopped"); 151 } 152 } 153 154 157 private void removeAllJAXRConnections() { 158 159 for (Iterator i = bindedFactories.keySet().iterator(); i.hasNext();) { 160 String name = (String ) i.next(); 161 removeJAXRConnection(name); 162 } 163 164 } 165 166 169 public void createJAXRConnection(Properties props) throws ServiceException { 170 if (logger.isLoggable(BasicLevel.DEBUG)) { 171 PropDump.print("These are the properties from which the jaxrService picks to construct jaxr Factories", props, logger, BasicLevel.DEBUG); 172 } 173 174 String jndiName = props.getProperty(JNDI_NAME); 176 177 if (jndiName == null) { 178 String err = "The property '" + JNDI_NAME + "' is a required property."; 179 logger.log(BasicLevel.ERROR, err); 180 throw new ServiceException(err); 181 } 182 183 if (bindedFactories.containsKey(jndiName)) { 185 String err = "There is already a factory bound with the name " + jndiName + ", please correct the provided configuration properties"; 186 logger.log(BasicLevel.ERROR, err); 187 throw new ServiceException(err); 188 } 189 190 JAXRConnection jaxrConnection = new JAXRConnection(props); 192 193 bindJAXRFactory(jndiName, jaxrConnection); 195 196 logger.log(BasicLevel.INFO, "Mapping JAXR Connection Factory on " + jndiName); 197 198 } 199 200 205 private void bindJAXRFactory(String jndiName, JAXRConnection jaxrConnection) { 206 try { 207 ictx.rebind(jndiName, jaxrConnection.getReference()); 208 bindedFactories.put(jndiName, jaxrConnection); 209 } catch (NamingException e) { 210 String err = "Cannot bind jaxr factory '" + jndiName + "'"; 211 logger.log(BasicLevel.ERROR, err); 212 throw new ServiceException(err, e); 213 } 214 } 215 216 219 public void modifyJAXRConnection(String name, JAXRConnection jaxrc) throws ServiceException { 220 removeJAXRConnection(name); 222 bindJAXRFactory(name, jaxrc); 224 if (logger.isLoggable(BasicLevel.DEBUG)) { 225 logger.log(BasicLevel.DEBUG, "JAXRConnection modified"); 226 } 227 } 228 229 232 public void removeJAXRConnection(String name) throws ServiceException { 233 234 if (!bindedFactories.containsKey(name)) { 236 String err = "Unknown JAXRConnection '" + name + "'"; 237 logger.log(BasicLevel.ERROR, err); 238 throw new ServiceException(err); 239 } 240 241 bindedFactories.remove(name); 243 244 try { 246 ictx.unbind(name); 247 } catch (NamingException e) { 248 String err = "Cannot unbind JAXR Connection '" + name + "'"; 249 logger.log(BasicLevel.ERROR, err); 250 throw new ServiceException(err, e); 251 } 252 253 if (logger.isLoggable(BasicLevel.DEBUG)) { 254 logger.log(BasicLevel.DEBUG, "JAXRConnection '" + name + "' removed"); 255 } 256 } 257 258 } 259 | Popular Tags |