1 22 package org.objectweb.petals.binding.xquarebc; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Properties ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 import javax.jbi.component.ComponentContext; 33 import javax.jbi.management.DeploymentException; 34 import javax.xml.namespace.QName ; 35 36 import org.objectweb.petals.component.common.serviceunitmanager.handler.ExternalServiceManager; 37 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager; 38 import org.objectweb.petals.component.common.util.ManagementMessageUtil; 39 import org.objectweb.petals.component.common.util.WSDLHelper; 40 import org.objectweb.petals.component.common.util.XMLHelper; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Node ; 43 import org.xquark.bridge.XQBridge; 44 import org.xquark.jdbc.datasource.JDBCDataSource; 45 46 61 public class XQuareSUHandler extends ExternalServiceManager { 62 63 public static final String JNDI_DATASOURCE = "jndidatasource.name"; 64 public static final String DATABASE_URL = "database.url"; 65 public static final String DATABASE_USER = "database.user"; 66 public static final String DATABASE_PASSWORD = "database.password"; 67 public static final String DATABASE_MAPPING = "database.mapping"; 68 public static final String XML_CONNECTION_BASE_URI = "xmlconnection.baseuri"; 69 public static final String WRAP_QUERY_RESULTS_IN_ROOT_TAG = "queryResults.wrapInRootTag"; 70 public static final String XQUARE_BRIDGE = "xquare_bridge_object"; 71 public static final String STANDALONE_DATASOURCE = "standalone_datasource_object"; 72 73 public static final String LISTENER_PROP_PREFIX = "newdatalistener."; 74 public static final String LISTENER_SERVICE_PROP = LISTENER_PROP_PREFIX + "service"; 75 public static final String LISTENER_OPERATION_PROP = LISTENER_PROP_PREFIX + "operation"; 76 77 protected HashMap <String , Properties > serviceToPropertiesMap; 78 protected HashMap <String , String > serviceUnitToServiceMap; 79 80 public XQuareSUHandler() { 81 super(); 82 SERVICE_UNIT_TYPE = "xquare_service"; 83 } 84 85 public XQuareSUHandler(ComponentContext context, Logger logger, 86 HashMap <String , Properties > serviceToPropertiesMap) { 87 super(context, logger); 88 this.context = context; 89 this.logger = logger; 90 this.serviceToPropertiesMap = serviceToPropertiesMap; 91 this.serviceUnitToServiceMap = new HashMap <String , String >(); 92 SERVICE_UNIT_TYPE = "xquare_service"; 93 } 94 95 98 public String deploy(String serviceUnitName, String serviceUnitType, 99 String serviceUnitRootPath) { 100 return super.deploy(serviceUnitName, serviceUnitType, 101 serviceUnitRootPath); 102 } 103 104 public void init(String serviceUnitName, String serviceUnitRootPath) 105 throws DeploymentException { 106 super.init(serviceUnitName, serviceUnitRootPath); 107 } 108 109 public String undeploy(String serviceUnitName, String serviceUnitRootPath) 110 throws DeploymentException { 111 String serviceName = this.serviceUnitToServiceMap.get(serviceUnitName); 113 Properties serviceProps = serviceToPropertiesMap.get(serviceName); 114 XQBridge bridge = (XQBridge) serviceProps.get(XQUARE_BRIDGE); 115 if (bridge != null) { 116 try { 117 bridge.close(); 118 } catch (Throwable t) { 119 logger.warning("Error closing bridge when undeploying service " + serviceName); 120 } 121 } 122 JDBCDataSource xquareDataSource = (JDBCDataSource) serviceProps.get(STANDALONE_DATASOURCE); 124 if (xquareDataSource != null) { 125 try { 126 xquareDataSource.close(); 127 } catch (Throwable t) { 128 logger.warning("Error closing datasource when undeploying service " + serviceName); 129 } 130 } 131 return ManagementMessageUtil.getComponentTaskResult(context 132 .getComponentName(), "undeploy", 133 ManagementMessageUtil.TASK_RESULT_SUCCESS); 134 } 135 136 public void shutDown(String serviceUnitName, 137 PetalsServiceUnitManager epHandler) throws DeploymentException { 138 super.shutDown(serviceUnitName, epHandler); 139 } 140 141 public void start(String serviceUnitName, PetalsServiceUnitManager epHandler) 142 throws DeploymentException { 143 String serviceUnitRootPath = serviceUnitInstallationRootPath 144 .get(serviceUnitName); 145 File [] files = new File (serviceUnitRootPath).listFiles(); 146 Document serviceDesc = null; 147 String propFileName = null; 148 QName service= null; 149 150 for (File file : files) { 151 if (file.getName().endsWith(".wsdl")) { 152 serviceDesc = WSDLHelper.createDocumentFromWSDL(file); 154 } else if (file.getName().endsWith(".properties")){ 155 propFileName = file.getAbsolutePath(); 157 } 158 } 159 160 if (serviceDesc != null) { 162 try { 163 activateEndpointsFromJBIDescription(epHandler, serviceDesc, 164 serviceUnitName, serviceUnitRootPath); 165 service = WSDLHelper.getServiceNameFromWSDLDocument(serviceDesc)[0]; 166 } catch (Exception ex) { 167 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT + ex); 168 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, ex); 169 } 170 } else { 171 throw new DeploymentException(INCOMPLETE_SERVICE_UNIT_PACKAGE); 172 } 173 174 175 if (service!=null && propFileName != null) { 177 Properties xquareProps = new Properties (); 178 try { 179 xquareProps.load(new FileInputStream (propFileName)); 180 } catch (IOException e) { 181 logger.log(Level.SEVERE, FAILED_ACTIVATE_ENDPOINT, e); 182 throw new DeploymentException(FAILED_ACTIVATE_ENDPOINT, e); 183 } 184 185 String databaseMappingFilepath = xquareProps.getProperty(DATABASE_MAPPING, "[none]"); 188 File databaseMappingFile = new File (databaseMappingFilepath); 189 if (!databaseMappingFile.exists() && !databaseMappingFile.isAbsolute()) { 190 databaseMappingFilepath = serviceUnitRootPath 192 + File.separatorChar + databaseMappingFilepath; 193 databaseMappingFile = new File (databaseMappingFilepath); 194 xquareProps.setProperty(DATABASE_MAPPING, databaseMappingFilepath); 195 } 196 if (!databaseMappingFile.exists() || databaseMappingFile.isDirectory() 197 || !databaseMappingFile.canRead()) { 198 throw new DeploymentException("Unable to find database mapping file at " 199 + databaseMappingFilepath); 200 } 201 if (!xquareProps.contains(XML_CONNECTION_BASE_URI)) { 203 xquareProps.setProperty(XML_CONNECTION_BASE_URI, "file://" + serviceUnitRootPath); 204 } 205 206 serviceUnitToServiceMap.put(serviceUnitName, service.getLocalPart()); 208 serviceToPropertiesMap.put(service.getLocalPart(), xquareProps); 209 } 210 211 232 } 233 234 public void stop(String serviceUnitName, PetalsServiceUnitManager epHandler) 235 throws DeploymentException { 236 super.stop(serviceUnitName, epHandler); 237 } 238 239 247 protected void initConfig(File jbiXml) { 248 Properties configProperties = new Properties (); 249 Document jbiDoc = WSDLHelper.createDocumentFromWSDL(jbiXml); 250 Node node = XMLHelper.findChild(jbiDoc, "services", true); 251 if (node != null) { 252 253 Node providesNode = XMLHelper.findChild(node, "provides", true); 254 if (providesNode != null) { 255 Node databaseUrlNode = XMLHelper.findChild(node, 256 "database_url", true); 257 258 if (databaseUrlNode != null) { 259 String databaseUrl = databaseUrlNode.getTextContent(); 260 configProperties.put(DATABASE_URL, databaseUrl); 261 } 262 263 } 265 266 Node consumesNode = XMLHelper.findChild(node, "consumes", true); 267 if (consumesNode != null) { 268 269 String service = XMLHelper.getAttributeValue(consumesNode, 270 "service-name"); 271 if (service != null) { 272 configProperties.put(XQuareSUHandler.LISTENER_SERVICE_PROP, service); 273 } 274 275 String operation = XMLHelper.getAttributeValue(consumesNode, 276 "operation-name"); 277 if (operation != null) { 278 configProperties.put(XQuareSUHandler.LISTENER_OPERATION_PROP, operation); 279 } 280 281 } 283 284 } 285 } 286 287 } 288 | Popular Tags |