|
tutorial applicationThis page describes the tutorial application's components that are common despite the storage used support. For details on filters, services and filter chains definitions please refer to Using OJB and Using XML sections. Please keep in mind that this application is very simple and quite useless. Keeping the tutorial application as simple as it gets allows readers to keep their focus on BERSERK. The full code for the tutorial application can be found under BERSERK's distribution root directory. For an example about using XML storage you might look in DigesterSample directory. OJB storage application is under SampleOJB directory. servicepublic class StdoutWriter implements IService { public void run(String string) { System.out.println(string); } } The service class has only one method that prints a string (passed as an
argument) to the standard output.
filtersA filter hierarchy was built, to exemplify the semantic coherence check: public abstract class AccessControlFilter implements IFilter { } This is the filter super class that will define a filter chain class. It adds
no functionality and it is used with the only propose of avoiding mixing
different filter types (access control filters with logging filters, etc). The
following code defines another filter type:
public abstract class LoggingFilter implements IFilter { } Two concrete filters that actually add access control and logging
functionality to the application can be built as the following:
public class RequesterIsIntegerFilter extends AccessControlFilter { public void execute(ServiceRequest request, ServiceResponse response) throws FilterException { if (request.getRequester() instanceof String) try { new Integer((String) request.getRequester()); } catch (NumberFormatException e) { throw new AccessDeniedException( "could not convert " + request.getRequester() + " to integer"); } else throw new AccessDeniedException( "could not convert " + request.getRequester() + " to integer"); } } This filter checks if the requester is a string and if it can be successfully
converted to a valid integer. If any of these conditions fails, an exception is
thrown. As said before, all exceptions thrown by filters that are intended to
be processed by BERSERK must extend BERSERK’s FilterException.
public class AccessLogger extends LoggingFilter { public void execute(ServiceRequest request, ServiceResponse response) throws FilterException { PrintWriter print_writer; try { print_writer = new PrintWriter(new FileWriter("logs")); print_writer.println( "Mr." + request.getRequester() + " accessed "+ request.getService()+ " at " + new Date(System.currentTimeMillis())); print_writer.flush(); print_writer.close(); } catch (IOException e) { //here you can throw a access denied and, for example, rollback database changes throw new RuntimeException("could not write log"); } } The code above implements a filter that logs accesses to a file. The thrown
exceptions are simple extensions for BERSERK’s FilterException.
mainThe application body is very simple and consists in a single call of the service. Because BERSERK is a monitor we need to execute the service class via the Service Manager class. The code that reads the requester from the standard input is omitted as it is a simple read from the keyboard: public static void main(String[] mainArgs) throws IOException, ServiceManagerException,FilterManagerException { try { Main.readRequester(); IServiceManager sm; sm = ServiceManager.getInstance(); //our service will be called with the string we want to write to the stdout Object[] args = {"Hello World"}; sm.execute(Main.requester,"StdoutWriter",args); } catch (ServiceManagerException e) { if (e instanceof FilterChainFailedException) System.out.println("Access Denied."); else throw e; } } This code tries to execute the StdoutWriter service. The catch statement
checks whether the caught exception signals an unsuccessful filter chain, in
which case the program has run correctly and an access denied message should be
printed. If another exception is thrown then an error occurred in the Service
Manager. Below are two interaction examples with the application that
illustrates the filter behaviour:
The filter behaved as expected. The log file was also created in
the successful case. Obviously this is a useless BERSERK application and its
only propose is to exemplify library usage in a simple scenario. More
elaborated applications follow the same approach, using more complex filters
and services.
|