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: */package org.apache.geronimo.corba;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.gbean.GBeanLifecycle;
021: import org.apache.geronimo.gbean.InvalidConfigurationException;
022: import org.apache.geronimo.system.serverinfo.ServerInfo;
023:
024: import org.apache.geronimo.corba.security.config.ConfigAdapter;
025:
026: import java.net.InetSocketAddress;
027:
028: /**
029: * Starts the openejb transient cos naming service.
030: * <p/>
031: * <gbean name="NameServer" class="org.apache.geronimo.corba.NameService">
032: * <reference name="ServerInfo">
033: * <reference name="ConfigAdapter">
034: * <attribute name="port">2809</attribute>
035: * <attribute name="host">localhost</attribute>
036: * </gbean>
037: *
038: * @version $Revision: 465108 $ $Date: 2006-10-17 17:23:40 -0700 (Tue, 17 Oct 2006) $
039: */
040: public class NameService implements GBeanLifecycle {
041: private static final Log log = LogFactory.getLog(NameService.class);
042:
043: // the ORB configurator
044: private final ConfigAdapter config;
045: // the name service instance
046: private Object service;
047: // the name service listening port
048: private final int port;
049: // the published port name (defaults to "localhost").
050: private String host;
051: // indicates whether we start and host this server locally.
052: private boolean localServer;
053:
054: protected NameService() {
055: service = null;
056: config = null;
057: port = -1;
058: host = "localhost";
059: localServer = true;
060: }
061:
062: /**
063: * GBean constructor to create a NameService instance.
064: *
065: * @param serverInfo The dependent ServerInfo. This value is not used,
066: * but is in the constructor to create an ordering
067: * dependency.
068: * @param config The ORB ConfigAdapter used to create the real
069: * NameService instance.
070: * @param host The advertised host name.
071: * @param port The listener port.
072: *
073: * @exception Exception
074: */
075: public NameService(ServerInfo serverInfo, ConfigAdapter config,
076: 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: * Return the NameService locator as a URI (generally
145: * using the corbaloc:: protocol);
146: *
147: * @return The URI in String format.
148: */
149: public String getURI() {
150: return "corbaloc::" + host + ":" + port + "/NameService";
151: }
152:
153: /**
154: * Start the NameService instance. If the local
155: * setting is true, will launch an appropriate
156: * in-process name server instance.
157: *
158: * @exception Exception
159: */
160: public void doStart() throws Exception {
161: if (localServer) {
162: try {
163: service = config.createNameService(host, port);
164: log
165: .debug("Started transient CORBA name service on port "
166: + port);
167: } catch (NoSuchMethodError e) {
168: log
169: .error("Incorrect level of org.omg.CORBA classes found.\nLikely cause is an incorrect java.endorsed.dirs configuration");
170: throw new InvalidConfigurationException(
171: "CORBA usage requires Yoko CORBA spec classes in java.endorsed.dirs classpath",
172: e);
173: }
174: }
175: }
176:
177: /**
178: * Stop the name server. Only has an effect if doStart()
179: * launched an NameServer instance.
180: *
181: * @exception Exception
182: */
183: public void doStop() throws Exception {
184: if (service != null) {
185: config.destroyNameService(service);
186: log.debug("Stopped transient CORBA name service on port "
187: + port);
188: }
189: }
190:
191: public void doFail() {
192: if (service != null) {
193: config.destroyNameService(service);
194: log.warn("Failed transient CORBA name service on port "
195: + port);
196: }
197: }
198: }
|