001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.corba; 018 019 import org.apache.commons.logging.Log; 020 import org.apache.commons.logging.LogFactory; 021 import org.apache.geronimo.gbean.GBeanLifecycle; 022 import org.apache.geronimo.gbean.InvalidConfigurationException; 023 import org.apache.geronimo.system.serverinfo.ServerInfo; 024 025 import org.apache.geronimo.corba.security.config.ConfigAdapter; 026 027 import java.net.InetSocketAddress; 028 029 /** 030 * Starts the openejb transient cos naming service. 031 * <p/> 032 * <gbean name="NameServer" class="org.apache.geronimo.corba.NameService"> 033 * <reference name="ServerInfo"> 034 * <reference name="ConfigAdapter"> 035 * <attribute name="port">2809</attribute> 036 * <attribute name="host">localhost</attribute> 037 * </gbean> 038 * 039 * @version $Revision: 465108 $ $Date: 2006-10-17 17:23:40 -0700 (Tue, 17 Oct 2006) $ 040 */ 041 public class NameService implements GBeanLifecycle { 042 private static final Log log = LogFactory.getLog(NameService.class); 043 044 // the ORB configurator 045 private final ConfigAdapter config; 046 // the name service instance 047 private Object service; 048 // the name service listening port 049 private final int port; 050 // the published port name (defaults to "localhost"). 051 private String host; 052 // indicates whether we start and host this server locally. 053 private boolean localServer; 054 055 protected NameService() { 056 service = null; 057 config = null; 058 port = -1; 059 host = "localhost"; 060 localServer = true; 061 } 062 063 /** 064 * GBean constructor to create a NameService instance. 065 * 066 * @param serverInfo The dependent ServerInfo. This value is not used, 067 * but is in the constructor to create an ordering 068 * dependency. 069 * @param config The ORB ConfigAdapter used to create the real 070 * NameService instance. 071 * @param host The advertised host name. 072 * @param port The listener port. 073 * 074 * @exception Exception 075 */ 076 public NameService(ServerInfo serverInfo, ConfigAdapter config, String host, int port) throws Exception { 077 this.host = host; 078 this.port = port; 079 this.config = config; 080 localServer = true; 081 service = null; 082 // if not specified, our default host is "localhost". 083 if (this.host == null) { 084 this.host = "localhost"; 085 } 086 } 087 088 /** 089 * Retrieve the host name for this NameService instance. 090 * 091 * @return The String host name. 092 */ 093 public String getHost() { 094 return host; 095 } 096 097 /** 098 * Get the port information for this NameService instance. 099 * 100 * @return The configured name service listener port. 101 */ 102 public int getPort() { 103 return port; 104 } 105 106 /** 107 * Get the "local" value for this server. If true, an 108 * in-process NameService instance will be created when 109 * the service is started. If false, this is an 110 * indirect reference to a NameService (possibly located 111 * elsewhere). 112 * 113 * @return The current localServer value. The default is 114 * true. 115 */ 116 public boolean getLocal() { 117 return localServer; 118 } 119 120 /** 121 * Get the "local" value for this server. If true, an 122 * in-process NameService instance will be created when 123 * the service is started. If false, this is an 124 * indirect reference to a NameService (possibly located 125 * elsewhere). 126 * 127 * @param l The new local setting. 128 */ 129 public void setLocal(boolean l) { 130 localServer = l; 131 } 132 133 /** 134 * Get the InetSocketAddress for this NameService. 135 * 136 * @return An InetSocketAddress containing the host and port 137 * information. 138 */ 139 public InetSocketAddress getAddress() { 140 return new InetSocketAddress(host, getPort()); 141 } 142 143 144 /** 145 * Return the NameService locator as a URI (generally 146 * using the corbaloc:: protocol); 147 * 148 * @return The URI in String format. 149 */ 150 public String getURI() { 151 return "corbaloc::" + host + ":" + port + "/NameService"; 152 } 153 154 /** 155 * Start the NameService instance. If the local 156 * setting is true, will launch an appropriate 157 * in-process name server instance. 158 * 159 * @exception Exception 160 */ 161 public void doStart() throws Exception { 162 if (localServer) { 163 try { 164 service = config.createNameService(host, port); 165 log.debug("Started transient CORBA name service on port " + port); 166 } catch (NoSuchMethodError e) { 167 log.error("Incorrect level of org.omg.CORBA classes found.\nLikely cause is an incorrect java.endorsed.dirs configuration"); 168 throw new InvalidConfigurationException("CORBA usage requires Yoko CORBA spec classes in java.endorsed.dirs classpath", e); 169 } 170 } 171 } 172 173 /** 174 * Stop the name server. Only has an effect if doStart() 175 * launched an NameServer instance. 176 * 177 * @exception Exception 178 */ 179 public void doStop() throws Exception { 180 if (service != null) { 181 config.destroyNameService(service); 182 log.debug("Stopped transient CORBA name service on port " + port); 183 } 184 } 185 186 public void doFail() { 187 if (service != null) { 188 config.destroyNameService(service); 189 log.warn("Failed transient CORBA name service on port " + port); 190 } 191 } 192 }