List of usage examples for java.rmi.registry LocateRegistry getRegistry
public static Registry getRegistry(String host, int port) throws RemoteException
Registry
on the specified host
and port
. From source file:org.apache.jackrabbit.j2ee.RepositoryStartupServlet.java
/** * Registers the repository to an RMI registry configured in the web * application. See <a href="#registerAlgo">Registration with RMI</a> in the * class documentation for a description of the algorithms used to register * the repository with an RMI registry.//w w w.jav a 2 s .c o m * @throws ServletException if an error occurs. */ private void registerRMI() { RMIConfig rc = config.getRmiConfig(); if (!rc.isValid() || !rc.enabled()) { return; } // try to create remote repository Remote remote; try { Class<?> clazz = Class.forName(getRemoteFactoryDelegaterClass()); RemoteFactoryDelegater rmf = (RemoteFactoryDelegater) clazz.newInstance(); remote = rmf.createRemoteRepository(repository); } catch (RemoteException e) { log.warn("Unable to create RMI repository.", e); return; } catch (Throwable t) { log.warn("Unable to create RMI repository." + " The jcr-rmi jar might be missing.", t); return; } try { System.setProperty("java.rmi.server.useCodebaseOnly", "true"); Registry reg = null; // first try to create the registry, which will fail if another // application is already running on the configured host/port // or if the rmiHost is not local try { // find the server socket factory: use the default if the // rmiHost is not configured RMIServerSocketFactory sf; if (rc.getRmiHost().length() > 0) { log.debug("Creating RMIServerSocketFactory for host " + rc.getRmiHost()); InetAddress hostAddress = InetAddress.getByName(rc.getRmiHost()); sf = getRMIServerSocketFactory(hostAddress); } else { // have the RMI implementation decide which factory is the // default actually log.debug("Using default RMIServerSocketFactory"); sf = null; } // create a registry using the default client socket factory // and the server socket factory retrieved above. This also // binds to the server socket to the rmiHost:rmiPort. reg = LocateRegistry.createRegistry(rc.rmiPort(), null, sf); rmiRegistry = reg; } catch (UnknownHostException uhe) { // thrown if the rmiHost cannot be resolved into an IP-Address // by getRMIServerSocketFactory log.info("Cannot create Registry", uhe); } catch (RemoteException e) { // thrown by createRegistry if binding to the rmiHost:rmiPort // fails, for example due to rmiHost being remote or another // application already being bound to the port log.info("Cannot create Registry", e); } // if creation of the registry failed, we try to access an // potentially active registry. We do not check yet, whether the // registry is actually accessible. if (reg == null) { log.debug("Trying to access existing registry at " + rc.getRmiHost() + ":" + rc.getRmiPort()); try { reg = LocateRegistry.getRegistry(rc.getRmiHost(), rc.rmiPort()); } catch (RemoteException re) { log.warn("Cannot create the reference to the registry at " + rc.getRmiHost() + ":" + rc.getRmiPort(), re); } } // if we finally have a registry, register the repository with the // rmiName if (reg != null) { log.debug("Registering repository as " + rc.getRmiName() + " to registry " + reg); reg.bind(rc.getRmiName(), remote); // when successfull, keep references this.rmiRepository = remote; log.info("Repository bound via RMI with name: " + rc.getRmiUri()); } else { log.info("RMI registry missing, cannot bind repository via RMI"); } } catch (RemoteException e) { log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e); } catch (AlreadyBoundException e) { log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e); } }
From source file:org.apache.juddi.api.impl.API_091_RMISubscriptionListenerIntegrationTest.java
@BeforeClass public static void startManager() throws ConfigurationException { org.apache.juddi.Registry.start(); try {/*w ww .j av a 2 s .com*/ //random port randomPort = 19800 + new Random().nextInt(99); System.out.println("RMI Random port=" + randomPort); //bring up the RMISubscriptionListener URI rmiEndPoint = new URI("rmi://localhost:" + randomPort + "/tck/rmisubscriptionlistener"); registry = LocateRegistry.createRegistry(rmiEndPoint.getPort()); path = rmiEndPoint.getPath(); //starting the service rmiSubscriptionListenerService = new UDDISubscriptionListenerImpl(0); //binding to the RMI Registry registry.bind(path, rmiSubscriptionListenerService); //double check that the service is bound in the local Registry Registry registry2 = LocateRegistry.getRegistry(rmiEndPoint.getHost(), rmiEndPoint.getPort()); registry2.lookup(rmiEndPoint.getPath()); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); Assert.fail(); } //manager = new UDDIClient(); //manager.start(); logger.debug("Getting auth tokens.."); try { api010.saveJoePublisher(); UDDISecurityPortType security = new UDDISecurityImpl(); authInfoJoe = TckSecurity.getAuthToken(security, TckPublisher.getJoePublisherId(), TckPublisher.getJoePassword()); Assert.assertNotNull(authInfoJoe); } catch (Exception e) { logger.error(e.getMessage(), e); Assert.fail("Could not obtain authInfo token."); } }
From source file:org.apache.juddi.v3.tck.JUDDI_091_RMISubscriptionListenerIntegrationTest.java
@BeforeClass public static void startManager() throws ConfigurationException { Assume.assumeTrue(TckPublisher.isEnabled()); try {//from www. ja v a 2 s . com //bring up the RMISubscriptionListener //random port randomPort = 19800 + new Random().nextInt(99); System.out.println("RMI Random port=" + randomPort); //bring up the RMISubscriptionListener URI rmiEndPoint = new URI("rmi://localhost:" + randomPort + "/tck/rmisubscriptionlistener"); registry = LocateRegistry.createRegistry(rmiEndPoint.getPort()); String path = rmiEndPoint.getPath(); hostname = InetAddress.getLocalHost().getHostName(); //starting the service rmiSubscriptionListenerService = new UDDISubscriptionListenerImpl(0); //binding to the RMI Registry registry.bind(path, rmiSubscriptionListenerService); //double check that the service is bound in the local Registry Registry registry2 = LocateRegistry.getRegistry(rmiEndPoint.getHost(), rmiEndPoint.getPort()); registry2.lookup(rmiEndPoint.getPath()); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); Assert.fail(); } manager = new UDDIClient(); manager.start(); logger.debug("Getting auth tokens.."); try { Transport transport = manager.getTransport("uddiv3"); UDDISecurityPortType security = transport.getUDDISecurityService(); authInfoJoe = TckSecurity.getAuthToken(security, TckPublisher.getJoePublisherId(), TckPublisher.getJoePassword()); UDDISubscriptionPortType subscription = transport.getUDDISubscriptionService(); UDDIPublicationPortType publication = transport.getUDDIPublishService(); UDDIInquiryPortType inquiry = transport.getUDDIInquiryService(); tckTModel = new TckTModel(publication, inquiry); tckBusiness = new TckBusiness(publication, inquiry); tckBusinessService = new TckBusinessService(publication, inquiry); rmiSubscriptionListener = new TckSubscriptionListenerRMI(subscription, publication); if (!TckPublisher.isUDDIAuthMode()) { TckSecurity.setCredentials((BindingProvider) publication, TckPublisher.getRootPublisherId(), TckPublisher.getRootPassword()); TckSecurity.setCredentials((BindingProvider) subscription, TckPublisher.getRootPublisherId(), TckPublisher.getRootPassword()); TckSecurity.setCredentials((BindingProvider) inquiry, TckPublisher.getRootPublisherId(), TckPublisher.getRootPassword()); } } catch (Exception e) { logger.error(e.getMessage(), e); Assert.fail("Could not obtain authInfo token."); } }
From source file:org.apache.juddi.v3.tck.UDDI_090_RMIIntegrationTest.java
@BeforeClass public static void startup() throws Exception { if (!TckPublisher.isEnabled()) { return;/* www . j a va2 s .co m*/ } startManager(); int count = 0; rmiSubscriptionListenerService = new UDDISubscriptionListenerImpl(0); UDDISubscriptionListenerImpl.notifcationMap.clear(); UDDISubscriptionListenerImpl.notificationCount = 0; while (true && count < 5) { try { count++; randomPort = 19800 + new Random().nextInt(99); System.out.println("RMI Random port=" + randomPort); //bring up the RMISubscriptionListener URI rmiEndPoint = new URI("rmi://localhost:" + randomPort + "/tck/rmisubscriptionlistener"); registry = LocateRegistry.createRegistry(rmiEndPoint.getPort()); String path = rmiEndPoint.getPath(); hostname = InetAddress.getLocalHost().getHostName(); //starting the service //binding to the RMI Registry registry.bind(path, rmiSubscriptionListenerService); //double check that the service is bound in the local Registry Registry registry2 = LocateRegistry.getRegistry(rmiEndPoint.getHost(), rmiEndPoint.getPort()); registry2.lookup(rmiEndPoint.getPath()); break; } catch (Exception ex) { logger.warn("trouble starting rmi endpoint " + ex.getMessage()); } } Assert.assertNotNull(registry); Assert.assertNotNull(hostname); }
From source file:org.hippoecm.repository.RepositoryServlet.java
@Override public void init(ServletConfig config) throws ServletException { super.init(config); hippoEventBus = new GuavaHippoEventBus(); HippoServiceRegistry.registerService(hippoEventBus, HippoEventBus.class); listener = new AuditLogger(); HippoServiceRegistry.registerService(listener, HippoEventBus.class); parseInitParameters(config);/*from www . j av a 2 s . c o m*/ System.setProperty(SYSTEM_SERVLETCONFIG_PROPERTY, repositoryConfig); try { // get the local embedded repository repository = HippoRepositoryFactory.getHippoRepository(storageLocation); HippoRepositoryFactory.setDefaultRepository(repository); if (startRemoteServer) { // the the remote repository RepositoryUrl url = new RepositoryUrl(bindingAddress); rmiRepository = new ServerServicingAdapterFactory(url) .getRemoteRepository(repository.getRepository()); System.setProperty("java.rmi.server.useCodebaseOnly", "true"); // Get or start registry and bind the remote repository try { registry = LocateRegistry.getRegistry(url.getHost(), url.getPort()); registry.rebind(url.getName(), rmiRepository); // connection exception happens here log.info("Using existing rmi server on " + url.getHost() + ":" + url.getPort()); } catch (ConnectException e) { registry = LocateRegistry.createRegistry(url.getPort()); registry.rebind(url.getName(), rmiRepository); log.info("Started an RMI registry on port " + url.getPort()); registryIsEmbedded = true; } } HippoServiceRegistry.registerService(repositoryService = (RepositoryService) repository.getRepository(), RepositoryService.class); HippoServiceRegistry.registerService(repositoryClusterService = new RepositoryClusterService() { @Override public boolean isExternalEvent(final Event event) { if (!(event instanceof JackrabbitEvent)) { throw new IllegalArgumentException("Event is not an instance of JackrabbitEvent"); } return ((JackrabbitEvent) event).isExternal(); } }, RepositoryClusterService.class); } catch (MalformedURLException ex) { log.error("MalformedURLException exception: " + bindingAddress, ex); throw new ServletException("RemoteException: " + ex.getMessage()); } catch (RemoteException ex) { log.error("Generic remoting exception: " + bindingAddress, ex); throw new ServletException("RemoteException: " + ex.getMessage()); } catch (RepositoryException ex) { log.error("Error while setting up JCR repository: ", ex); throw new ServletException("RepositoryException: " + ex.getMessage()); } freeMarkerConfiguration = createFreemarkerConfiguration(); }
From source file:org.kchine.rpf.KillAll.java
public static void main(String[] args) throws Exception { String stub = System.getProperty("stub"); if (stub == null || stub.equals("")) { Registry rmiRegistry = null; final String dburl = System.getProperty("db.url"); if (dburl != null && !dburl.equals("")) { final String user = System.getProperty("db.user"); final String password = System.getProperty("db.password"); System.out.println("DB url:" + dburl); System.out.println("DB user:" + user); System.out.println("DB password:" + password); Class.forName(System.getProperty("db.driver")); rmiRegistry = DBLayer.getLayer(getDBType(dburl), new ConnectionProvider() { public Connection newConnection() throws java.sql.SQLException { return DriverManager.getConnection(dburl, user, password); };//ww w . j a v a 2s . c o m }); } else { rmiRegistry = LocateRegistry.getRegistry(_registryHost, _registryPort); } String[] servantNames = rmiRegistry.list(); for (int i = 0; i < servantNames.length; ++i) { if (PoolUtils.shortRmiName(servantNames[i]).startsWith(_servantPoolPrefix)) { try { ((ManagedServant) rmiRegistry.lookup(servantNames[i])).die(); } catch (Exception e) { // e.printStackTrace(); } } } } else { ((ManagedServant) PoolUtils.hexToStub(stub, KillAll.class.getClassLoader())).die(); } }
From source file:org.kchine.rpf.reg.ServantProviderFactoryReg.java
public ServantProviderFactoryReg() { if (System.getProperty("pools.regmode.configuration.file") != null && !System.getProperty("pools.regmode.configuration.file").equals("")) { try {/*w ww.j a v a2s .com*/ initPoolsHashMap(new FileInputStream(System.getProperty("pools.regmode.configuration.file"))); } catch (Exception e) { e.printStackTrace(); } } else if (System.getProperty("pools.regmode.configuration.resource") != null && !System.getProperty("pools.regmode.configuration.resource").equals("")) { try { initPoolsHashMap(this.getClass() .getResourceAsStream(System.getProperty("pools.regmode.configuration.resource"))); } catch (Exception e) { e.printStackTrace(); } } else { String poolPrefix = ServerDefaults._servantPoolPrefix; String registryHost = ServerDefaults._registryHost; int registryPort = ServerDefaults._registryPort; int timeOutMillisec = System.getProperty("timeout") != null && !System.getProperty("timeout").equals("") ? Integer.decode(System.getProperty("timeout")) : DEFAULT_TIMEOUT; _defaultPoolName = "DEFAULT"; PoolData poolData = new PoolData(_defaultPoolName, timeOutMillisec, new Vector<PoolNode>()); poolData.getNodes().add(new PoolNode(poolPrefix, registryHost, registryPort)); _poolHashMap.put(poolData.getPoolName(), poolData); try { _rmiRegistry = LocateRegistry.getRegistry(registryHost, registryPort); } catch (Exception e) { e.printStackTrace(); } } System.out.println(_poolHashMap); _servantProvider = new ServantProvider() { public ManagedServant borrowServantProxy(String poolName) throws TimeoutException { ManagedServant proxy = null; int nodesSize = _poolHashMap.get(poolName).getNodes().size(); Vector<Integer> order = PoolUtils.getRandomOrder(nodesSize); int nodeIndex = 0; PoolNode node = null; long tstart = System.currentTimeMillis(); do { try { node = _poolHashMap.get(poolName).getNodes().elementAt(order.elementAt(nodeIndex)); proxy = (ManagedServant) ServantProxyPoolSingletonReg.getInstance(node.getRegistryHost(), node.getRegistryPort(), node.getServantPoolPrefix()).borrowObject(); } catch (NoSuchElementException e) { } catch (Exception ex) { ex.printStackTrace(); } if (proxy != null) { try { log.info("<" + Thread.currentThread().getName() + "> obtained resource : " + proxy.getServantName()); } catch (Exception e) { } break; } else { nodeIndex = (nodeIndex + 1) % nodesSize; } if (System.currentTimeMillis() - tstart > _poolHashMap.get(poolName).getBorrowTimeout()) throw new TimeoutException(); try { Thread.sleep(20); } catch (Exception e) { } log.info("<" + Thread.currentThread().getName() + "> thread waiting for resource for : " + ((System.currentTimeMillis() - tstart) / 1000) + " seconds"); } while (true); _borrowedServants.put(proxy, node); return proxy; } public ManagedServant borrowServantProxyNoWait(String poolName) { ManagedServant proxy = null; int nodesSize = _poolHashMap.get(poolName).getNodes().size(); Vector<Integer> order = PoolUtils.getRandomOrder(nodesSize); int nodeIndex = 0; PoolNode node = null; do { try { node = _poolHashMap.get(poolName).getNodes().elementAt(order.elementAt(nodeIndex)); proxy = (ManagedServant) ServantProxyPoolSingletonReg.getInstance(node.getRegistryHost(), node.getRegistryPort(), node.getServantPoolPrefix()).borrowObject(); } catch (NoSuchElementException e) { } catch (Exception ex) { ex.printStackTrace(); } if (proxy != null) { try { log.info("<" + Thread.currentThread().getName() + "> obtained resource : " + proxy.getServantName()); } catch (Exception e) { } break; } else { nodeIndex = (nodeIndex + 1); if (nodeIndex >= nodesSize) break; } } while (true); if (proxy != null) _borrowedServants.put(proxy, node); return proxy; } public void returnServantProxy(ManagedServant proxy) { if (proxy == null) return; try { PoolNode node = _borrowedServants.get(proxy); _borrowedServants.remove(proxy); ServantProxyPoolSingletonReg.getInstance(node.getRegistryHost(), node.getRegistryPort(), node.getServantPoolPrefix()).returnObject(proxy); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } public void returnServantProxy(String poolName, ManagedServant proxy) { if (proxy == null) return; try { PoolNode node = _borrowedServants.get(proxy); _borrowedServants.remove(proxy); ServantProxyPoolSingletonReg.getInstance(node.getRegistryHost(), node.getRegistryPort(), node.getServantPoolPrefix()).returnObject(proxy); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } public ManagedServant borrowServantProxy() throws TimeoutException { return borrowServantProxy(_defaultPoolName); } public ManagedServant borrowServantProxyNoWait() { return borrowServantProxyNoWait(_defaultPoolName); } public String getDefaultPoolName() { return _defaultPoolName; } public Registry getRegistry() { return _rmiRegistry; } }; }
From source file:org.kchine.rpf.reg.ServantsProxyFactoryReg.java
public ServantsProxyFactoryReg(String registryHost, int registryPort, String poolNamingPrefix) { super();/* www .j a va2 s . c o m*/ _poolNamingPrefix = poolNamingPrefix; try { _rmiRegistry = LocateRegistry.getRegistry(registryHost, registryPort); _rmiRegistry.list(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.kchine.rpf.ServerDefaults.java
public static Registry getRmiRegistry() throws Exception { if (_registry != null) return _registry; synchronized (_lock) { if (_registry == null) { if (_namingMode.equals("db")) { Class.forName(_dbDriver); _registry = DBLayer.getLayer(PoolUtils.getDBType(_dbUrl), new ConnectionProvider() { public Connection newConnection() throws java.sql.SQLException { return DriverManager.getConnection(_dbUrl, _dbUser, _dbPassword); };/* w ww. j av a 2 s. c om*/ }); } else if (_namingMode.equals("self")) { _registry = LocalRmiRegistry.getInstance(); } else if (_namingMode.equals("registry")) { _registry = LocateRegistry.getRegistry(_registryHost, _registryPort); } else { _registry = ((RegistryProvider) ServerDefaults.class .forName("genericnaming." + _namingMode + "Class").newInstance()) .getRegistry(System.getProperties()); } } return _registry; } }
From source file:org.kchine.rpf.ServerDefaults.java
public static Registry getRegistry(Properties props) throws Exception { String namingMode;/* w w w.j a va2 s.c o m*/ String registryHost; int registryPort; String dbUrl = null; String dbDriver = null; String dbUser; String dbPassword; namingMode = (String) props.get("naming.mode") != null && !props.get("naming.mode").equals("") ? (String) props.get("naming.mode") : DEFAULT_NAMING_MODE; registryHost = (String) props.get("registry.host") != null && !props.get("registry.host").equals("") ? (String) props.get("registry.host") : DEFAULT_REGISTRY_HOST; registryPort = (String) props.get("registry.port") != null && !props.get("registry.port").equals("") ? Integer.decode((String) props.get("registry.port")) : DEFAULT_REGISTRY_PORT; String _DB_TYPE = (String) props.get("db.type") != null && !props.get("db.type").equals("") ? (String) props.get("db.type") : DEFAULT_DB_TYPE; String _DB_HOST = (String) props.get("db.host") != null && !props.get("db.host").equals("") ? (String) props.get("db.host") : DEFAULT_DB_HOST; int _DB_PORT = (String) props.get("db.port") != null && !props.get("db.port").equals("") ? Integer.decode((String) props.get("db.port")) : DEFAULT_DB_PORT; String _DB_NAME = (String) props.get("db.name") != null && !props.get("db.name").equals("") ? (String) props.get("db.name") : DEFAULT_DB_NAME; String _DB_DIR = (String) props.get("db.dir") != null && !props.get("db.dir").equals("") ? (String) props.get("db.dir") : DEFAULT_DB_DIR; _DB_DIR = _DB_DIR.replace('\\', '/'); if (!_DB_DIR.equals("") && !_DB_DIR.endsWith("/")) _DB_DIR = _DB_DIR + "/"; System.out.println("_DB_TYPE:" + _DB_TYPE); if (_DB_TYPE.equals("derby")) { dbUrl = "jdbc:derby://" + _DB_HOST + ":" + _DB_PORT + "/" + _DB_DIR + _DB_NAME + ";create=true"; dbDriver = "org.apache.derby.jdbc.ClientDriver"; } else if (_DB_TYPE.equals("mysql")) { dbUrl = "jdbc:mysql://" + _DB_HOST + ":" + _DB_PORT + "/" + _DB_NAME; dbDriver = "org.gjt.mm.mysql.Driver"; } else if (_DB_TYPE.equals("oracle")) { dbUrl = "jdbc:oracle:thin:@" + _DB_HOST + ":" + _DB_PORT + ":" + _DB_NAME; dbDriver = "oracle.jdbc.driver.OracleDriver"; } dbUser = (String) props.get("db.user") != null && !props.get("db.user").equals("") ? (String) props.get("db.user") : DEFAULT_DB_USER; dbPassword = (String) props.get("db.password") != null && !props.get("db.password").equals("") ? (String) props.get("db.password") : DEFAULT_DB_PASSWORD; Registry registry = null; if (namingMode.equals("db")) { Class.forName(dbDriver); final String dbUrlFinal = dbUrl; final String dbUserFinal = dbUser; final String dbPasswordFinal = dbPassword; registry = DBLayer.getLayer(PoolUtils.getDBType(dbUrl), new ConnectionProvider() { public Connection newConnection() throws java.sql.SQLException { return DriverManager.getConnection(dbUrlFinal, dbUserFinal, dbPasswordFinal); }; }); } else if (namingMode.equals("self")) { registry = LocalRmiRegistry.getInstance(); } else if (namingMode.equals("registry")) { registry = LocateRegistry.getRegistry(registryHost, registryPort); } else { registry = ((RegistryProvider) ServerDefaults.class.forName("genericnaming." + namingMode + "Class") .newInstance()).getRegistry(props); } return registry; }