/*
Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
This file is part of the JODB (Java Objects Database) open source project.
JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.mobixess.jodb.util.rmi;
import java.io.IOException;
import java.rmi.AccessException;
import java.rmi.RemoteException;
import java.rmi.dgc.DGC;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.mobixess.jodb.core.JODBConfig;
import com.mobixess.jodb.core.JodbNetConstants;
import com.mobixess.jodb.util.Utils;
public class RegistryManager {
private static RegistryManager _instance = new RegistryManager();
private Registry _registry;
private int _requestsCounter;
private RegistryManager() {
}
public static RegistryManager getInstance() {
return _instance;
}
public synchronized void createRegistry() throws IOException{
if(_requestsCounter == 0){
_registry = LocateRegistry.createRegistry(JodbNetConstants.DEFAULT_RMI_PORT);
}
_requestsCounter++;
}
public synchronized void shutdownRegistry() throws IOException{
_requestsCounter--;
if(_requestsCounter < 0){
throw new IOException();
}
if(_requestsCounter==0){
UnicastRemoteObject.unexportObject(_registry, true);
//System.gc();
if(JODBConfig.DEBUG){
printRegistryList();
Logger logger = Utils.getLogger(getClass().getName());
logger.log(Level.INFO,"Shutdown Registry - "+_registry);
}
_registry = null;
}
}
public void printRegistryList() throws IOException{
String[] regList = list();
Logger logger = Utils.getLogger(getClass().getName());
logger.log(Level.INFO,"Registry content <<<<<<<<<<<<<<<<<<<< ");
for (int i = 0; regList !=null && i < regList.length; i++) {
logger.log(Level.INFO,"Registry content -"+regList[i]);
}
logger.log(Level.INFO,"Registry content >>>>>>>>>>>>>>>>>>>> ");
}
public String[] list() throws IOException{
if(_requestsCounter<=0){
return null;
}
return _registry.list();
}
}