View Javadoc
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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.v3.client.config;
18  
19  import javax.servlet.ServletContext;
20  
21  import org.apache.commons.configuration.ConfigurationException;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * 
27   * @author kstam
28   */
29  public class WebHelper {
30  
31          public static final Log logger = LogFactory.getLog(WebHelper.class);
32          public static final String UDDI_CLIENT_NAME = "uddi.client.name";
33          public static final String UDDI_CLIENT_CONFIG_FILE = "uddi.client.config.file";
34          public static final String JUDDI_CLIENT_NAME = "juddi.client.name";
35          public static final String JUDDI_CLIENT_TRANSPORT = "juddi.client.transport";
36  
37          /**
38           * Checks the servlet context for the manager defined in the web
39           * context. Optionally, in your web.xml you can specify either the
40           * manager name if you want to use an existing manager called
41           * 'uddi-portlet-manager':
42           * <pre>
43           * &lt;context-param&gt;
44           *   &lt;param-name&gt;uddi.client.manager.name&lt;/param-name&gt;
45           *   &lt;param-value&gt;uddi-portlet-manager&lt;/param-value&gt;
46           * &lt;/context-param&gt;
47           * </pre> or, if you don't want to use the default META-INF/uddi.xml
48           * file path, but 'META-INF/my-uddi.xml' instead, then you can set:
49           * <pre>
50           * &lt;context-param&gt;
51           *   &lt;param-name&gt;uddi.client.config.path&lt;/param-name&gt;
52           *   &lt;param-value&gt;META-INF/my-uddi.xml&lt;/param-value&gt;
53           * &lt;/context-param&gt;
54           * </pre>
55           *
56           * @param servletContext
57           * @return a UDDI Client instance
58           * @throws ConfigurationException
59           */
60          public static UDDIClient getUDDIClient(ServletContext servletContext) throws ConfigurationException {
61                  if (servletContext.getAttribute(JUDDI_CLIENT_NAME) != null) {
62                          String clientName = String.valueOf(servletContext.getAttribute(JUDDI_CLIENT_NAME));
63                          return UDDIClientContainer.getUDDIClient(clientName);
64                  } else {
65                          String clientName = servletContext.getInitParameter(UDDI_CLIENT_NAME);
66                          if (clientName != null) {
67                                  try {
68                                          UDDIClient client = UDDIClientContainer.getUDDIClient(clientName);
69                                          logger.info("Client " + clientName + " was already started.");
70                                          servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
71                                          return 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);
77                          if (clientConfigFile == null) {
78                                  clientConfigFile = ClientConfig.DEFAULT_UDDI_CONFIG;
79                          }
80                          
81  
82                          logger.info("Reading the clientName from the clientConfig file " + clientConfigFile);
83                          UDDIClient client = new UDDIClient(clientConfigFile);
84                          if (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                          }
88                          if (client.getName() != null) {
89                                  logger.info("Starting Client " + client.getName() + "...");
90                                  clientName = client.getName();
91                          } else {
92                                  throw new ConfigurationException("A client name needs to be specified in the client config file.");
93                          }
94  
95                          client.start();
96                          servletContext.setAttribute(JUDDI_CLIENT_NAME, clientName);
97                          return client;
98                  }
99          }
100 }