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.servicemix.jbi.jmx;
018
019 import java.rmi.RemoteException;
020 import java.rmi.registry.LocateRegistry;
021 import java.rmi.registry.Registry;
022 import java.rmi.server.UnicastRemoteObject;
023
024 import org.springframework.beans.factory.DisposableBean;
025 import org.springframework.beans.factory.FactoryBean;
026 import org.springframework.beans.factory.InitializingBean;
027
028 /**
029 *
030 * @author gnodet
031 * @org.apache.xbean.XBean element="rmiRegistry"
032 */
033 public class RmiRegistryFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
034
035 private int port = Registry.REGISTRY_PORT;
036 private Registry registry;
037 private boolean locate;
038 private boolean create = true;
039 private boolean locallyCreated;
040
041 /**
042 * @return the create
043 */
044 public boolean isCreate() {
045 return create;
046 }
047
048 /**
049 * @param create the create to set
050 */
051 public void setCreate(boolean create) {
052 this.create = create;
053 }
054
055 /**
056 * @return the locate
057 */
058 public boolean isLocate() {
059 return locate;
060 }
061
062 /**
063 * @param locate the locate to set
064 */
065 public void setLocate(boolean locate) {
066 this.locate = locate;
067 }
068
069 /**
070 * @return the port
071 */
072 public int getPort() {
073 return port;
074 }
075
076 /**
077 * @param port the port to set
078 */
079 public void setPort(int port) {
080 this.port = port;
081 }
082
083 public Object getObject() throws Exception {
084 return registry;
085 }
086
087 public Class getObjectType() {
088 return Registry.class;
089 }
090
091 public boolean isSingleton() {
092 return true;
093 }
094
095 public void afterPropertiesSet() throws RemoteException {
096 if (registry == null && locate) {
097 try {
098 Registry reg = LocateRegistry.getRegistry(getPort());
099 reg.list();
100 registry = reg;
101 } catch (RemoteException e) {
102 // ignore
103 }
104 }
105 if (registry == null && create) {
106 registry = LocateRegistry.createRegistry(getPort());
107 locallyCreated = true;
108 }
109 }
110
111 public void destroy() throws RemoteException {
112 if (registry != null && locallyCreated) {
113 Registry reg = registry;
114 registry = null;
115 UnicastRemoteObject.unexportObject(reg, true);
116 }
117 }
118
119 }