1 /* 2 Copyright (c) 2006, University of Tromsø 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 8 * Redistributions of source code must retain the above copyright notice, this list 9 of conditions and the following disclaimer. 10 11 * Redistributions in binary form must reproduce the above copyright notice, this 12 list of conditions and the following disclaimer in the documentation and/or other 13 materials provided with the distribution. 14 15 * Neither the name of the University of Tromsø nor the names of its contributors may 16 be used to endorse or promote products derived from this software without specific 17 prior written permission. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 DAMAGE. 29 */ 30 31 package axis.dynamic; 32 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.Comparator; 36 import java.util.Iterator; 37 import java.util.List; 38 import java.util.concurrent.ConcurrentHashMap; 39 import java.util.concurrent.ConcurrentMap; 40 41 import javax.xml.namespace.QName; 42 43 import org.apache.axis.AxisEngine; 44 import org.apache.axis.ConfigurationException; 45 import org.apache.axis.EngineConfiguration; 46 import org.apache.axis.Handler; 47 import org.apache.axis.SimpleTargetedChain; 48 import org.apache.axis.configuration.SimpleProvider; 49 import org.apache.axis.deployment.wsdd.WSDDDeployment; 50 import org.apache.axis.description.ServiceDesc; 51 import org.apache.axis.handlers.http.URLMapper; 52 import org.apache.axis.handlers.soap.SOAPService; 53 import org.apache.axis.transport.http.AxisServlet; 54 55 56 /** 57 * Extends {@link SoapConfiguration}, adds ability to deploy / undeploy services and a few property setters. 58 */ 59 public class SoapServerConfiguration extends SoapConfiguration 60 { 61 62 /** the deployed services. */ 63 private final ConcurrentMap<QName, SOAPService> _services = new ConcurrentHashMap<QName, SOAPService>(); 64 65 /** 66 * Creates a server configuration with a http transport {@link Handler} with a {@link URLMapper}. 67 */ 68 public SoapServerConfiguration() 69 { 70 // we need to register a global transport with an URLMapper request handler. 71 // otherwise, Axis won't find our service methods. 72 deployTransport("http", new SimpleTargetedChain(new URLMapper(), null, null)); 73 } 74 75 /** 76 * @param disable 77 */ 78 public void setDisableServicesList( boolean disable ) 79 { 80 // synchronization shouldn't be necessary, it's a Hashtable and only modified on startup 81 _globalOptions.put(AxisServlet.INIT_PROPERTY_DISABLE_SERVICES_LIST, Boolean.toString(disable)); 82 } 83 84 /** 85 * necessary to tell axis which path to put into the links on generated html pages. 86 * @param path 87 */ 88 public void setServicesPath( String path ) 89 { 90 String fixed = path; 91 // Damn - currently, Axis absolutely needs these slashes... 92 if (! fixed.startsWith("/")) fixed = "/"+fixed; 93 if (! fixed.endsWith("/")) fixed = fixed+"/"; 94 // synchronization shouldn't be necessary, it's a Hashtable and only modified on startup 95 _globalOptions.put(AxisServlet.INIT_PROPERTY_SERVICES_PATH, fixed); 96 } 97 98 /** 99 * called from {@link AxisEngine#init()}. 100 * @param engine 101 * @throws ConfigurationException 102 * @see EngineConfiguration#configureEngine(AxisEngine) 103 * @see AxisEngine#init() 104 */ 105 @Override 106 public void configureEngine( AxisEngine engine ) throws ConfigurationException 107 { 108 // this also sets the _engine field 109 super.configureEngine(engine); 110 111 // note: this is reasonably thread safe - if a service is deployed after we call 112 // _services.values(), we check the _engine field and call setEngine() for the new 113 // service. it's possible that setEngine() is called twice, but that should be a no-op. 114 for (SOAPService service : _services.values()) 115 { 116 // this also delegates the type mappings of the services to our global type mapping registry. 117 service.setEngine(_engine); 118 } 119 } 120 121 /** 122 * @param name 123 * @param service 124 */ 125 public void deployService( String name, SOAPService service ) 126 { 127 SOAPService present = _services.putIfAbsent(new QName(name), service); 128 129 if (present != null) throw new IllegalArgumentException("duplicate service name: ["+name+"]"); 130 131 if (_engine != null) 132 { 133 service.setEngine(_engine); 134 } 135 136 137 } 138 139 /** 140 * @param name 141 */ 142 public void undeployService( String name ) 143 { 144 _services.remove(new QName(name)); 145 146 } 147 148 /** 149 * @param qname 150 * @return service for given name 151 * @see EngineConfiguration#getService(QName) 152 */ 153 public SOAPService getService( QName qname ) 154 { 155 return _services.get(qname); 156 } 157 158 /** 159 * This implementation currently just returns null. I don't know how namespace-based dispatching 160 * is supposed to work. It should be fairly easy to implement this method. See {@link WSDDDeployment}. 161 * The implementation in {@link SimpleProvider} seems to be broken (muddles up namspace and local name). 162 * @param namespace 163 * @return null 164 */ 165 public SOAPService getServiceByNamespaceURI( String namespace ) 166 { 167 return null; 168 } 169 170 /** 171 * returns an enumeration of the services currently deployed to this engine 172 * @return an enumeration of the services currently deployed to this engine 173 */ 174 public Iterator<ServiceDesc> getDeployedServices() 175 { 176 List<ServiceDesc> serviceDescs = new ArrayList<ServiceDesc>(); 177 for (SOAPService service : _services.values()) 178 { 179 serviceDescs.add(service.getServiceDescription()); 180 } 181 182 // sort by service name 183 Collections.sort(serviceDescs, SERVICE_DESC_COMPARATOR); 184 185 return serviceDescs.iterator(); 186 } 187 188 /** compares service descriptions by their name, ignoring case */ 189 private static final Comparator<ServiceDesc> SERVICE_DESC_COMPARATOR = 190 new Comparator<ServiceDesc>() 191 { 192 public int compare( ServiceDesc desc1, ServiceDesc desc2 ) 193 { 194 return desc1.getName().compareToIgnoreCase(desc2.getName()); 195 } 196 }; 197 198 }