Objectives:
Overview:
In this laboratory you will write and test a shared file repository based on CORBA. The project is divided on three parts:
FileRepository object from a main program.
Note: Prior to starting this laboratory you should be sure that you have successfully run the HelloWorld client and server from the Java tutorial.
Requirements:
Part I: The nonremote implementation
As part of this assignment, you will need to implement two interfaces:
the Directory and the FileObject
(e.g., DirectoryImpl and FileObjectImpl).
In Parts II and III, the Directory and FileObject
interfaces will become CORBA IDL modules.
DirectoryImplNonRemote class
that implements the Directory interface:
public interface Directory {
public void create(String filename) throws Exception;
public FileObject getFile(String filename);
public String [] getListing( );
public void remove(String filename);
public void update(FileObject f) throws Exception;
}
Your implementation should keep a list of the filenames
of the FileObjects that have already
been created in the repository. Your implementation should keep
the filename, FileObject pairs in a hash table.
You may implement the following alternative interface instead of the
one above. You may implement as many of the update methods as you
wish.
public interface Directory {
public void create(String filename) throws Exception;
public FileObject getFile(String filename);
public String [] getListing( );
public void remove(String filename);
public void update(FileObject f) throws Exception;
public void update(FileObject f, byte [] b) throws Exception;
public void update(String name, byte [] b) throws Exception;
}
create method creates a new
FileObject object, puts the filename
in its list
of filenames and puts the FileObject in the hash table
using the filename as the hash key.
The create method throws an Exception if a
FileObject with that filename already
exists in the repository or if the object creation or insertion into
the repository fails.
getFile method returns a FileObject
with the current contents of the file indicated by filename.
The getFile method returns a null when a
FileObject with that filename does not
exist in the repository.
getListing method returns an array with the names
of the FileObjects currently in the repository.
The listing should be in alphabetical order. If the
repository is empty, a zero-length array should be returned.
remove method removes the FileObject
with the designated name
from the repository.
update method replaces the data of FileObject
of the same name as f with data of f. The
read and
write methods of the FileObject are used.
The
update method throws an Exception if the
object is not in the repository.
FileObjectImpl class that
implements the FileObject interface:
public interface FileObject {
public String getName();
public int getSize();
public int getVersion( );
public byte [] read( ) throws Exception;
public void write (byte [] b) throws Exception;
}
The FileObject
represents a file that has been put in the repository.
getName method returns the filename
of the object.
getSize method returns the number of
bytes of data in the file represented by the object.
getVersion method returns the
version number of the file represented by the object.
read method returns an array of bytes
containing a copy of the data in the file. The read
method throws an Exception if there was an
error in reading the file or in creating the array of bytes
to return.
write method replaces the data in the
file with the bytes from the array b and
increments the version number. The write method
also updates the size.
The write method throws an
Exception if it is unable perform the replacement.
Your FileObjectImpl should have a constructor
that takes a String parameter filename.
A newly created FileObjectImp must initially be
empty. You will call the write method to
actually put data in the object.
main method of DirectoryImpl
and FileObjectImpl or
write a separate test classes.
Part II: Remote version with no synchronization
In Part II you will convert the Directory interface and the
DirectoryImpl class to support remote invocation via CORBA.
Your server will act as the repository and allow remote invocation
of the methods in the Directory interface.
Implement the FileObject as a CORBA interface. Thus, the
getFile method of the Directory interface
will return a remote object reference to the FileObject.
Part III: Synchronization for multiple clients
If more than one client accesses your remote objects, you must provide synchronization.
Each java object has its own monitor. When you declare a method to be synchronized,
the code within the method is executed mutually exclusively with the other
synchronized methods in the object. The simplest way to handle synchronization,
is to declare each method to be synchronized. This approach can
be very inefficient. To receive full credit, you must implement a readers-writer
type synchronization for your DirectoryImpl. You must also include
in your writeup a discussion of your approach.
Test your synchronization by creating multiple simultaneous clients that access the repository.
Part IV: Object Serialization
This part of the assignment explores object serialization. Provide a second
implementation of the FileObject interface that stores the actual
data of the object in a file. The write method first creates
a new local file to hold the data before removing the old one.
Your implementation should include
a finalize method that removes the
local data file associated with the FileObject.
The finalize method will be called when the
object is garbage collected.
Because you will be passing FileObject as a remote
reference, you will not need the serialization. However, you still need to
implement and test the serialization.
The added complication in this
problem is that the default technique for serialization will probably not work,
because data from the object is kept on disk and local files only have meaning
in a local environment. You will need to define your own methods with
the following signature:
private void readObject (ObjectInputStream in) throws
IOException, ClassNotFoundException;
private void writeObject (ObjectOutputStream out)
throws IOException;
You can call:
in.defaultReadObject( );
in the readObject to read in the default serialization first.
You can call:
out.defaultWriteObject( );
in the writeObject method to write the default serialization
before writing the special information.
Note: You must test the serialization separately before using this class in your remote programs.