00001
00026 package org.objectweb.jonas.jaxr;
00027
00028 import java.util.Hashtable;
00029 import java.util.Iterator;
00030 import java.util.Map;
00031 import java.util.Properties;
00032 import java.util.Vector;
00033
00034 import javax.naming.Context;
00035 import javax.naming.InitialContext;
00036 import javax.naming.NamingException;
00037
00038 import org.objectweb.jonas.common.JProp;
00039 import org.objectweb.jonas.common.Log;
00040 import org.objectweb.jonas.common.PropDump;
00041 import org.objectweb.jonas.service.AbsServiceImpl;
00042 import org.objectweb.jonas.service.ServiceException;
00043
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045 import org.objectweb.util.monolog.api.Logger;
00046
00047
00052 public class JAXRServiceImpl extends AbsServiceImpl implements JAXRService {
00053
00057 private Context ictx = null;
00061 private static Logger logger = null;
00062
00066 private Vector factoryNames = new Vector();
00067
00071 private Map bindedFactories = new Hashtable();
00075 public static final String FACTORIES = "jonas.service.jaxr.factories";
00076
00080 public static final String CLASS = "jonas.service.jaxr.class";
00081
00085 private static final String JNDI_NAME = "jaxr.jndi.name";
00086
00090 protected void doInit(Context ctx) throws ServiceException {
00091
00092 logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
00093
00094
00095 try {
00096 ictx = new InitialContext();
00097 } catch (NamingException e) {
00098 logger.log(BasicLevel.ERROR, "Cannot create initial context during the jaxr service initializing");
00099 throw new ServiceException("Cannot create initial context during the jaxr service initializing", e);
00100 }
00101
00102
00103 String factories = null;
00104 try {
00105 factories = (String) ctx.lookup(FACTORIES);
00106 } catch (NamingException e) {
00107 ;
00108 }
00109 if (factories != null) {
00110 String[] names = factories.split(", ");
00111 for (int i = 0; i < names.length; i++) {
00112 factoryNames.add(names[i].trim());
00113 }
00114 }
00115 logger.log(BasicLevel.DEBUG, "jaxr service initialized");
00116
00117 }
00118
00122 protected void doStart() throws ServiceException {
00123
00124 String factoryName = null;
00125 for (Iterator i = factoryNames.iterator(); i.hasNext();) {
00126 factoryName = (String) i.next();
00127 try {
00128 JProp prop = JProp.getInstance(factoryName);
00129 if (logger.isLoggable(BasicLevel.DEBUG)) {
00130 logger.log(BasicLevel.DEBUG, "Creating JAXR Connection Factory " + factoryName);
00131 }
00132 createJAXRConnection(prop.getConfigFileEnv());
00133 } catch (Exception e) {
00134 if (logger.isLoggable(BasicLevel.ERROR)) {
00135 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create jaxr factory " + factoryName + " : " + e);
00136 logger.log(BasicLevel.ERROR, "Please check the " + factoryName + ".properties file");
00137 }
00138 }
00139 }
00140 }
00141
00145 protected void doStop() throws ServiceException {
00146 removeAllJAXRConnections();
00147 logger.log(BasicLevel.DEBUG, "jaxr service stopped");
00148 }
00149
00153 private void removeAllJAXRConnections() {
00154
00155 for (Iterator i = bindedFactories.keySet().iterator(); i.hasNext();) {
00156 String name = (String) i.next();
00157 removeJAXRConnection(name);
00158 }
00159
00160 }
00161
00165 public void createJAXRConnection(Properties props) throws ServiceException {
00166 PropDump.print("These are the properties from which the jaxrService picks to construct jaxr Factories", props, logger, BasicLevel.DEBUG);
00167
00168
00169 String jndiName = props.getProperty(JNDI_NAME);
00170
00171 if (jndiName == null) {
00172 String err = "The property '" + JNDI_NAME + "' is a required property.";
00173 logger.log(BasicLevel.ERROR, err);
00174 throw new ServiceException(err);
00175 }
00176
00177
00178 if (bindedFactories.containsKey(jndiName)) {
00179 String err = "There is already a factory bound with the name " + jndiName + ", please correct the provided configuration properties";
00180 logger.log(BasicLevel.ERROR, err);
00181 throw new ServiceException(err);
00182 }
00183
00184
00185 JAXRConnection jaxrConnection = new JAXRConnection(props);
00186
00187
00188 bindJAXRFactory(jndiName, jaxrConnection);
00189
00190 logger.log(BasicLevel.INFO, "Mapping JAXR Connection Factory on " + jndiName);
00191
00192 }
00193
00199 private void bindJAXRFactory(String jndiName, JAXRConnection jaxrConnection) {
00200 try {
00201 ictx.rebind(jndiName, jaxrConnection.getReference());
00202 bindedFactories.put(jndiName, jaxrConnection);
00203 } catch (NamingException e) {
00204 String err = "Cannot bind jaxr factory '" + jndiName + "'";
00205 logger.log(BasicLevel.ERROR, err);
00206 throw new ServiceException(err, e);
00207 }
00208 }
00209
00213 public void modifyJAXRConnection(String name, JAXRConnection jaxrc) throws ServiceException {
00214
00215 removeJAXRConnection(name);
00216
00217 bindJAXRFactory(name, jaxrc);
00218
00219 logger.log(BasicLevel.DEBUG, "JAXRConnection modified");
00220 }
00221
00225 public void removeJAXRConnection(String name) throws ServiceException {
00226
00227
00228 if (!bindedFactories.containsKey(name)) {
00229 String err = "Unknown JAXRConnection '" + name + "'";
00230 logger.log(BasicLevel.ERROR, err);
00231 throw new ServiceException(err);
00232 }
00233
00234
00235 bindedFactories.remove(name);
00236
00237
00238 try {
00239 ictx.unbind(name);
00240 } catch (NamingException e) {
00241 String err = "Cannot unbind JAXR Connection '" + name + "'";
00242 logger.log(BasicLevel.ERROR, err);
00243 throw new ServiceException(err, e);
00244 }
00245
00246 logger.log(BasicLevel.DEBUG, "JAXRConnection '" + name + "' removed");
00247 }
00248
00249 }