JvnServerImpl.java

Go to the documentation of this file.
00001 
00009 package jvn;
00010 
00011 import java.rmi.server.UnicastRemoteObject;
00012 import java.util.HashMap;
00013 import java.io.*;
00014 
00015 import java.rmi.Naming;
00016 import java.rmi.NotBoundException;
00017 import java.rmi.RemoteException;
00018 
00022 public class JvnServerImpl      extends UnicastRemoteObject implements JvnLocalServer, JvnRemoteServer{
00026         private static final long serialVersionUID = -1834717182805714127L;
00027 
00031         private static JvnServerImpl js = null;
00032 
00036         private HashMap<Integer,JvnObject> jvnObjectIds = null;
00037 
00041         private JvnRemoteCoord jvnCoordinator = null;
00042 
00046         private JvnServerImpl() throws Exception {
00047                 jvnObjectIds = new HashMap<Integer,JvnObject>();
00048                 try {
00049                         jvnCoordinator = (JvnRemoteCoord)Naming.lookup("AT_LD_jvnCoordinator");
00050                 } catch(NotBoundException e) {
00051                         System.out.println("Coordinator not found!");
00052                         jvnCoordinator = null;
00053                 }
00054         }
00055 
00063         public static JvnServerImpl jvnGetServer() {
00064                 if (js == null) {
00065                         try {
00066                                 js = new JvnServerImpl();
00067                         } catch (Exception e) {
00068                                 System.out.println("Error creating the Javanaise Server!\n"+e);
00069                                 return null;
00070                         }
00071                 }
00072                 return js;
00073         }
00074 
00078         public void jvnTerminate() throws jvn.JvnException {
00079                 JvnRemoteCoord coord = null;
00080                 synchronized(this) {
00081                         if(jvnCoordinator!=null) {
00082                                 coord = jvnCoordinator;
00083                                 jvnCoordinator = null;
00084                         }
00085                 }
00086 
00087                 System.out.println("Terminating the Javanaise Server");
00088 
00089                 try {
00090                         if (coord != null) {
00091                                 coord.jvnServerTerminated(this);
00092                         }
00093                 } catch (RemoteException e) {}
00094         }
00095 
00107         public JvnObject jvnCreateObject(Serializable o)
00108         throws jvn.JvnException {
00109                 return new JvnObjectImpl(o);
00110         }
00111 
00121         public void jvnRegisterObject(String jon, JvnObject jo) throws jvn.JvnException {
00122                 if(jvnCoordinator == null) {
00123                         throw new JvnException("Coordinator is not present!");
00124                 } else if(jo == null || jon == null) {
00125                         throw new JvnException("The given name or JvnObject is not valid!");
00126                 } else {
00127                         try {
00128                                 int id = jvnCoordinator.jvnGetObjectId();
00129                                 jo.jvnSetObjectId(id);
00130                                 jvnCoordinator.jvnRegisterObject(jon,jo,this);
00131 
00132                                 // Only put in cache after successful registration
00133                                 jvnObjectIds.put(id,jo);
00134                         } catch (RemoteException e) {
00135                                 throw new JvnException("Network error while registering object!\n" + e);
00136                         }
00137                 }
00138         }
00139 
00149         public JvnObject jvnLookupObject(String jon, Class<? extends JvnObject> type) throws jvn.JvnException {
00150                 /*if(jvnCoordinator == null) {
00151                         throw new JvnException("Coordinator is not present!");
00152                 }*/
00153                 if (jon == null) {
00154                         throw new JvnException("The given name is not valid!");
00155                 }
00156                 if (type == null) {
00157                         throw new JvnException("The given class type is not valid!");
00158                 }
00159                 else {
00160                         try {
00161                                 //System.out.println(type.getName());
00162                                 JvnObject temp = jvnCoordinator.jvnLookupObject(jon, type, this);
00163                                 //System.out.println(temp);
00164                                 if (temp != null) {
00165                                         // A Javanaise object with name <jon> exists.
00166                                         
00167                                         // String-comparison-based type checking !
00168                                         if (temp.toString().startsWith(type.getName())) {
00169 //                                      if (temp.getClass().getName().startsWith(type.getName())) {
00170                                                 try {
00171                                                         // Try typecast...
00172                                                         // This is supposed to handle JvnObject vs. JvnObjectImpl,
00173                                                         // which string-comparison-based type checking cannot do
00174                                                         // cleanly.
00175                                                         temp.getClass().asSubclass(type);
00176                                                         // The Javanaise object with name <jon> has the same type as the
00177                                                         // given type, so we assume that it is the same object.
00178                                                         jvnObjectIds.put(temp.jvnGetObjectId(), temp);
00179                                                         ((JvnObjectImpl)temp).__containedObjectLockState = JvnObjectState.STATE_NOLOCK;
00180                                                 }
00181                                                 catch (ClassCastException e) {
00182                                                         System.err.println(type.getName());
00183                                                         System.err.println(temp.toString());
00184                                                         throw new JvnException("Object with same name, but different type, previously registered (2)!");
00185                                                 }
00186                                                 
00187                                         }
00188                                         else {
00189                                                 // Rare case: there is already a Javanaise object with name <jon>,
00190                                                 // but the type of that object is different from the given type...
00191                                                 // Trying to use the previously registered object may trigger all
00192                                                 // kinds of exceptions: ClassCastException, NoSuchMethodException, etc.
00193                                                 // We'd better signaling there's a problem.
00194                                                 System.err.println(type.getName());
00195                                                 System.err.println(temp.toString());
00196                                                 throw new JvnException("Object with same name, but different type, previously registered (1)!");
00197                                         }
00198                                 }
00199                                 
00200                                 //try {
00201                                         //c = (type.class)o;
00202                                         //type.class temp = (JvnObject)o;
00203                                         //if (o instanceof c)
00204                                         //Class.forName(className)
00205                                         if(temp!=null) {
00206                                         }
00207                                         return temp;
00208                                 //}
00209                         } catch(RemoteException e) {
00210                                 throw new JvnException("Network error!\n"+e);
00211                         } catch(ClassCastException e) {
00212                                 throw new JvnException("Unexpected class type!");
00213                         }
00214                 }
00215         }
00216 
00227         public Serializable jvnLockRead(int joi) throws JvnException {
00228                 if(jvnCoordinator == null) {
00229                         throw new JvnException("Coordinator is not present!");
00230                 } else if( jvnObjectIds.containsKey(joi) ) {
00231                         try {
00232                                 return jvnCoordinator.jvnLockRead(joi,this);
00233                         } catch (RemoteException e) {
00234                                 throw new JvnException("Network error while locking object for reading!\n" + e);
00235                         }
00236                 } else {
00237                         throw new JvnException("Unknown object ID! Plase call jvnLookupObject before calling this method!");
00238                 }
00239         }
00250         public Serializable jvnLockWrite(int joi) throws JvnException {
00251                 if(jvnCoordinator == null) {
00252                         throw new JvnException("Coordinator is not present!");
00253                 } else if( jvnObjectIds.containsKey(joi) ) {
00254                         try {
00255                                 return jvnCoordinator.jvnLockWrite(joi,this);
00256                         } catch (RemoteException e) {
00257                                 throw new JvnException("Network error while locking object for reading!\n" + e);
00258                         }
00259                 } else {
00260                         throw new JvnException("Unknown object ID! Plase call jvnLookupObject before calling this method!");
00261                 }
00262         }
00263 
00264 
00273         public void jvnInvalidateReader(int joi) throws java.rmi.RemoteException,jvn.JvnException {
00274                 JvnObject jo = jvnObjectIds.get(joi);
00275 
00276                 if( jo==null ) {
00277                         throw new JvnException("Object id not found while invalidating reader!");
00278                 } else {
00279                         jo.jvnInvalidateReader();
00280                 }
00281         }
00282 
00291         public Serializable jvnInvalidateWriter(int joi) throws java.rmi.RemoteException,jvn.JvnException {
00292                 JvnObject jo = jvnObjectIds.get(joi);
00293 
00294                 if( jo==null ) {
00295                         throw new JvnException("Object id not found while invalidating reader!");
00296                 } else {
00297                         return jo.jvnInvalidateWriter();
00298                 }
00299         }
00300 
00310         public Serializable jvnInvalidateWriterForReader(int joi) throws java.rmi.RemoteException,jvn.JvnException {
00311                 JvnObject jo = jvnObjectIds.get(joi);
00312 
00313                 if( jo==null ) {
00314                         throw new JvnException("Object id not found while invalidating reader!");
00315                 } else {
00316                         return jo.jvnInvalidateWriterForReader();
00317                 }
00318         }
00319 }

Generated on Wed Jan 2 10:15:54 2008 for Javanaise by  doxygen 1.5.4