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
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
00151
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
00162 JvnObject temp = jvnCoordinator.jvnLookupObject(jon, type, this);
00163
00164 if (temp != null) {
00165
00166
00167
00168 if (temp.toString().startsWith(type.getName())) {
00169
00170 try {
00171
00172
00173
00174
00175 temp.getClass().asSubclass(type);
00176
00177
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
00190
00191
00192
00193
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
00201
00202
00203
00204
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 }