examples.guestbook
Class GuestbookServiceImpl

java.lang.Object
  extended byde.fhg.igd.semoa.service.AbstractService
      extended byexamples.guestbook.GuestbookServiceImpl
All Implemented Interfaces:
GuestbookService, Service

public class GuestbookServiceImpl
extends AbstractService
implements GuestbookService

This realizes access to a guestbook. The guestbook itself is backed by a file consisting of key-value pairs for each single entry. In SeMoA this service should be published as follows. The command may be entered on the command line or stated in file etc/rc.local (in one line!)

 java de.fhg.igd.semoa.bin.Publish 
       -class examples.GuestbookServiceImpl 
       -key ${WhatiIs:APPLICATION_SERVICE}/guestbook 
       \; -setFile myguestbook.txt
 

Version:
$Id: GuestbookServiceImpl.java 1913 2007-08-08 02:41:53Z jpeters $
Author:
U. Pinsdorf

Nested Class Summary
(package private)  class GuestbookServiceImpl.PrivilegedLoad
          This privileged action loads the values from a given InputStream and stores them in a Properties instance.
(package private)  class GuestbookServiceImpl.PrivilegedStore
          This privileged action stores the values of an Properties instance to a given OutputSteam.
 
Field Summary
private static String ENTRY_KEY_PREFIX
          The entries in the guestbook file are represented as key-value pairs like mesg.12345="Hello dear!".
private  File file_
          References the file which backs the guestbook.
private  Object lock_
          This object is used for internal synchronization.
private  Logger LOG
          The logger instance for writing a log file.
private static String OWNER_KEY
          The name of the owner entry in the guestbook.
 
Fields inherited from class de.fhg.igd.semoa.service.AbstractService
REV_POSTFIX, REV_PREFIX
 
Fields inherited from interface examples.guestbook.GuestbookService
WHATIS
 
Constructor Summary
GuestbookServiceImpl()
           
 
Method Summary
 void addEntry(String text)
          Allows to add an entry to the end of the guestbook.
 String author()
          Returns the name of the author of the service.
 String[] getEntries()
          Returns all entries from the guestbook.
 String getOwner()
          Returns the name of the guestbook's owner.
private  String getUniqueKey()
          Generates a unique key for an entry in the guestbook file.
 String info()
          Returns the informative string which should describe the service's essence in a sentence.
private  Properties loadGuestbook()
          Reads the guestbook from the file and returns it a new instance which holds all key-value-pairs from the file.
 String revision()
          Returns the revision number of this class as a string.
private  void saveGuestbook(Properties gb)
           
 void setFile(String fn)
          Remembers the given file name as new guestbook file.
 String toString()
          Pretty prints the whole guestbook.
 
Methods inherited from class de.fhg.igd.semoa.service.AbstractService
checkDependencies, dependencies, docs, doPrivileged, doPrivileged, getACC, getEnvironment, majorVersion, minorVersion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENTRY_KEY_PREFIX

private static final String ENTRY_KEY_PREFIX
The entries in the guestbook file are represented as key-value pairs like mesg.12345="Hello dear!". All keys start with the string denoted in this constant. The constant is used by the method getUniqueKey().

See Also:
Constant Field Values

OWNER_KEY

private static final String OWNER_KEY
The name of the owner entry in the guestbook. All entries in the guestbook are represented as key-value pairs. An entry like owner=Snoopy states that 'Snoopy' is the name of the guestbook's owner.

See Also:
getOwner(), Constant Field Values

LOG

private final Logger LOG
The logger instance for writing a log file. For the logger's configuration refer to file etc/semoa.logging.


file_

private File file_
References the file which backs the guestbook. The file can be set by means of setFile(String).


lock_

private final Object lock_
This object is used for internal synchronization.

See Also:
loadGuestbook(), saveGuestbook(Properties)
Constructor Detail

GuestbookServiceImpl

public GuestbookServiceImpl()
Method Detail

info

public String info()
Description copied from class: AbstractService
Returns the informative string which should describe the service's essence in a sentence.

Specified by:
info in interface Service
Specified by:
info in class AbstractService
Returns:
the String with the service's short description.
See Also:
AbstractService.info()

author

public String author()
Description copied from class: AbstractService
Returns the name of the author of the service. The format to be used should be John Doe <jdoe@zilch.net>.

Specified by:
author in interface Service
Specified by:
author in class AbstractService
Returns:
the String with the author's name.

revision

public String revision()
Description copied from class: AbstractService
Returns the revision number of this class as a string. This class provides default implementations of methods majorVersion(), minorVersion based on the assumption that this method returns a revision string as generated by rcs(1) for the template "$Revision: 1.7 $/$Date: 2007-08-08 04:41:53 +0200 (Wed, 08 Aug 2007) $".

Specified by:
revision in class AbstractService

toString

public String toString()
Pretty prints the whole guestbook. This method overrides Object.toString(). It is very useful to access the service from the SeMoA shell print ${WhatiIs:APPLICATION_SERVICE}/guestbook where the path corresponds to the path where the service is actually published.

Overrides:
toString in class AbstractService
Returns:
The string representation of this instance. This default implementation returns information on the author, version, revision, and general info.

getOwner

public String getOwner()
Description copied from interface: GuestbookService
Returns the name of the guestbook's owner. The data is read from the guetsbook itself. Hence it may change only if the guestbook gets altered.

Specified by:
getOwner in interface GuestbookService
Returns:
name of the guestbook owner
See Also:
GuestbookService.getOwner()

getEntries

public String[] getEntries()
Description copied from interface: GuestbookService
Returns all entries from the guestbook. The return value is a field of strings where each field member represents one entry.

Specified by:
getEntries in interface GuestbookService
Returns:
field of all guestbook entries
See Also:
GuestbookService.getEntries()

addEntry

public void addEntry(String text)
Description copied from interface: GuestbookService
Allows to add an entry to the end of the guestbook.

Specified by:
addEntry in interface GuestbookService
Parameters:
text - the entry to be added
See Also:
GuestbookService.addEntry(java.lang.String)

setFile

public void setFile(String fn)
Remembers the given file name as new guestbook file. The file will be accessed with the next guestbook operation. Caution: it is possible that the new guestbook get overwritten instantly by an addEntry(String) operation.

Parameters:
fn - absolute file name of the guestbook file
See Also:
loadGuestbook(), saveGuestbook(Properties)

loadGuestbook

private Properties loadGuestbook()
Reads the guestbook from the file and returns it a new instance which holds all key-value-pairs from the file. The guestbook is read in a synchronized method to guarantee a thread-safe reading. Anyway, we need to synchronize the calling methods again in addEntry(String),getEntries(), and getOwner(). This is because we cannot be sure that the guestbook returned by this method is still up-to-date in the next moment.

Returns:
the guestbook as stored in the file

saveGuestbook

private void saveGuestbook(Properties gb)

getUniqueKey

private String getUniqueKey()
Generates a unique key for an entry in the guestbook file. This method is used by addEntry(String)each time a new entry has to be added.

Returns:
the unique id


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.