View Javadoc

1   /*
2    * Copyright 2001-2004 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  package org.apache.juddi.registry.rmi;
17  
18  import java.util.Properties;
19  
20  import javax.naming.InitialContext;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.juddi.registry.RegistryEngine;
25  import org.apache.juddi.util.Config;
26  
27  /***
28   * @author Kurt Stam (kurt.stam@redhat.com)
29   */
30  public class JNDIRegistration
31  {
32  	/*** Instance of the InqueryService, so we have a running instance we 
33  	 *  can remotely attach to it later. */
34  	public static Inquiry mInquery=null;
35  	/*** Instance of the PublishService, so we have a running instance we
36  	 *  can remotely attach to it later. */
37  	public static Publish mPublish=null;
38  	/*** Name of the inquiry service in JNDI */
39  	public static String INQUIRY_SERVICE="/InquiryService";
40  	/*** Name of the publish service in JNDI */
41  	public static String PUBLISH_SERVICE="/PublishService";
42  	/*** Logger */
43  	private static Log log = LogFactory.getLog(JNDIRegistration.class);
44  	/***
45  	 * Registers the Publish and Inquiry Services to JNDI and instantiates a
46  	 * instance of each so we can remotely attach to it later.
47  	 */
48  	public static void register()
49  	{
50  		try {
51              //JNDI settings can be overriden with System parameters
52              String factoryInitial = System.getProperty(RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_INITIAL);
53              String providerURL    = System.getProperty(RegistryEngine.PROPNAME_JAVA_NAMING_PROVIDER_URL);
54              String factoryURLPkgs = System.getProperty(RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_URL_PKGS);
55  			//If not set then getting the JNDI setting from the config, or default them.
56              if (factoryInitial==null) { 
57                  factoryInitial = Config.getStringProperty(
58  					  RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_INITIAL,
59  					  RegistryEngine.DEFAULT_JAVA_NAMING_FACTORY_INITIAL);
60              } else {
61                  log.debug("FactoryInitial was obtained from a System Parameter");
62              }
63  			if (providerURL==null) { 
64                  providerURL = Config.getStringProperty(
65  					  RegistryEngine.PROPNAME_JAVA_NAMING_PROVIDER_URL,
66  					  RegistryEngine.DEFAULT_JAVA_NAMING_PROVIDER_URL);
67              } else {
68                  log.debug("ProviderUrl was obtained from a System Parameter");
69              }
70              
71  			if (factoryURLPkgs==null) {
72                  factoryURLPkgs = Config.getStringProperty(
73  					  RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_URL_PKGS,
74  					  RegistryEngine.DEFAULT_JAVA_NAMING_FACTORY_URL_PKGS);
75              } else {
76                  log.debug("FactoryURLPkgs was obtained from a System Parameter");
77              }
78  			//Setting them in the properties for the Initial Context 
79  			Properties env = new Properties();
80  			env.setProperty(RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_INITIAL, factoryInitial);
81  			env.setProperty(RegistryEngine.PROPNAME_JAVA_NAMING_PROVIDER_URL, providerURL);
82  			env.setProperty(RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_URL_PKGS, factoryURLPkgs); 
83  			if (log.isDebugEnabled()) {
84  			log.debug("Creating Initial Context using: \n" 
85  				+ RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_INITIAL + "=" + factoryInitial + "\n"
86  				+ RegistryEngine.PROPNAME_JAVA_NAMING_PROVIDER_URL    + "=" + providerURL + "\n"
87  				+ RegistryEngine.PROPNAME_JAVA_NAMING_FACTORY_URL_PKGS + "=" + factoryURLPkgs + "\n");
88  			}
89  			InitialContext context = new InitialContext(env);
90  			Inquiry inquiry = new InquiryService();
91  			if (log.isDebugEnabled()) {
92  				log.debug("Setting " + INQUIRY_SERVICE + ", " + inquiry.getClass().getName());
93  			}
94  			mInquery = inquiry;
95  			context.rebind(INQUIRY_SERVICE, inquiry);
96  			Publish publish = new PublishService();
97  			if (log.isDebugEnabled()) {
98  				log.debug("Setting " + PUBLISH_SERVICE + ", " + publish.getClass().getName());
99  			}
100 			mPublish = publish;
101 			context.rebind(PUBLISH_SERVICE, publish);
102 		} catch (Exception e) {
103 			e.printStackTrace();
104 		}
105 	}
106 }