1/*2 * Copyright 2001-2011 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17package org.apache.juddi.v3.client.config;
1819import javax.servlet.ServletContext;
2021import org.apache.commons.configuration.ConfigurationException;
22import org.apache.commons.logging.Log;
23import org.apache.commons.logging.LogFactory;
2425/**26 * 27 * @author kstam28 */29publicclassWebHelper {
3031publicstaticfinal Log logger = LogFactory.getLog(WebHelper.class);
32publicstaticfinal String UDDI_CLIENT_NAME = "uddi.client.name";
33publicstaticfinal String UDDI_CLIENT_CONFIG_FILE = "uddi.client.config.file";
34publicstaticfinal String JUDDI_CLIENT_NAME = "juddi.client.name";
35publicstaticfinal String JUDDI_CLIENT_TRANSPORT = "juddi.client.transport";
3637/**38 * Checks the servlet context for the manager defined in the web39 * context. Optionally, in your web.xml you can specify either the40 * manager name if you want to use an existing manager called41 * 'uddi-portlet-manager':42 * <pre>43 * <context-param>44 * <param-name>uddi.client.manager.name</param-name>45 * <param-value>uddi-portlet-manager</param-value>46 * </context-param>47 * </pre> or, if you don't want to use the default META-INF/uddi.xml48 * file path, but 'META-INF/my-uddi.xml' instead, then you can set:49 * <pre>50 * <context-param>51 * <param-name>uddi.client.config.path</param-name>52 * <param-value>META-INF/my-uddi.xml</param-value>53 * </context-param>54 * </pre>55 *56 * @param servletContext57 * @return a UDDI Client instance58 * @throws ConfigurationException59 */60publicstaticUDDIClient getUDDIClient(ServletContext servletContext) throws ConfigurationException {
61if (servletContext.getAttribute(JUDDI_CLIENT_NAME) != null) {
62 String clientName = String.valueOf(servletContext.getAttribute(JUDDI_CLIENT_NAME));
63return UDDIClientContainer.getUDDIClient(clientName);
64 } else {
65 String clientName = servletContext.getInitParameter(UDDI_CLIENT_NAME);
66if (clientName != null) {
67try {
68UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
69 logger.info("Client " + clientName + " was already started.");
70 servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
71return client;
72 } catch (ConfigurationException ce) {
73 logger.debug("Client " + clientName + " is not yet started.");
74 }
75 }
76 String clientConfigFile = servletContext.getInitParameter(UDDI_CLIENT_CONFIG_FILE);
77if (clientConfigFile == null) {
78 clientConfigFile = ClientConfig.DEFAULT_UDDI_CONFIG;
79 }
808182 logger.info("Reading the clientName from the clientConfig file " + clientConfigFile);
83UDDIClient client = newUDDIClient(clientConfigFile);
84if (client.getName() == null) {
85 logger.warn("Deprecated, client name set to 'default', however it should be provided in the uddi.xml");
86 clientName = "default";
87 }
88if (client.getName() != null) {
89 logger.info("Starting Client " + client.getName() + "...");
90 clientName = client.getName();
91 } else {
92thrownew ConfigurationException("A client name needs to be specified in the client config file.");
93 }
9495 client.start();
96 servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
97return client;
98 }
99 }
100 }