|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.values.ValuesConfigBean
public class ValuesConfigBean
A 'generic' ValuesConfig implementation for
beans (uses XML).
The ValuesConfigBean is used to to store and load the values
from bean style Java classes as XML files. This class takes care of
most of the headaches for you by using the magic provided by XMLEncoder and XMLDecoder. You just
need to worry about the following:
The following demonstrates how one might use this object to save and load a Email bean:
import com.ccg.values.ValuesConfigBean;
public class Email {
public static void main(String[] args) {
// Show usage if invalid args
if (args.length != 1 && args.length != 3) {
System.out.println("Usage: java Email ALIAS [NAME EMAIL]");
System.exit(1);
}
// Load configuration based on ALIAS
ValuesConfigBean vcb = new ValuesConfigBean(Email.class);
try {
vcb.load(args[0]);
} catch (Exception ignore) {
if (args.length == 1) {
System.err.println("No configuration found for:"+args[0]);
System.exit(2);
}
}
// Transfer values loaded into a new Email object
Email email = new Email();
vcb.toObject(email);
// If new values on command line
if (args.length == 3) {
// store them in the bean
email.setName(args[1]);
email.setEmail(args[2]);
try { // lets archive it for future availability
vcb.fromObject(email);
vcb.save(args[0]);
} catch (Exception saveErr) {
System.err.println("failed to save "+email+" under:"+args[0]);
System.exit(2);
}
}
// Display current state of bean
System.out.println("Email associated with:"+args[0]);
System.out.println(" "+email);
}
public Email() { _Name=_Email="unknown"; }
public String getName() { return _Name; }
public void setName(String name) { if (name != null) _Name = name; }
public String getEmail() { return _Email; }
public void setEmail(String email) { if (email != null) _Email = email; }
public String toString() { return _Name+" <"+_Email+'>'; }
private String _Name;
private String _Email;
}
The beauty of thie arrangement is that we really didn't have to worry too much on the mechanics of saving/loading the values of our bean. We just made sure it behaved like a good bean and then let the ValuesConfigBean do the rest of the work for us. The following shows some results from compiling and running the above example:
[pkb@salsa tmp]$ jikes Email.java [pkb@salsa tmp]$ java Email Usage: java Email ALIAS [NAME EMAIL] [pkb@salsa tmp]$ java Email paul No configuration found for:paul [pkb@salsa tmp]$ java Email paul "Paul Blankenbaker" paul@mekwin.com Email associated with:paul Paul Blankenbaker <paul@mekwin.com> [pkb@salsa tmp]$ java Email paul Email associated with:paul Paul Blankenbaker <paul@mekwin.com> [pkb@salsa tmp]$ java Email megan "Megan Brown" megan@mekwin.com Email associated with:megan Megan Brown <megan@mekwin.com> [pkb@salsa tmp]$ java Email paul Email associated with:paul Paul Blankenbaker <paul@mekwin.com> [pkb@salsa tmp]$ java Email megan Email associated with:megan Megan Brown <megan@mekwin.com> [pkb@salsa tmp]$ ls $HOME/.etc/jconf/*.Email /home/pkb/.etc/jconf/megan.Email /home/pkb/.etc/jconf/paul.Email [pkb@salsa tmp]$ cat $HOME/.etc/jconf/paul.Email <?xml version="1.0" encoding="UTF-8"?> <java version="1.4.2_02" class="java.beans.XMLDecoder"> <object class="Email"> <void property="email"> <string>paul@mekwin.com</string> </void> <void property="name"> <string>Paul Blankenbaker</string> </void> </object> </java> [pkb@salsa tmp]$
ValuesEditorBean| Field Summary | |
|---|---|
(package private) Method[] |
_GetMethods
|
(package private) Method[] |
_SetMethods
|
| Fields inherited from interface com.ccg.values.ValuesHolder |
|---|
HAS_FROM, HAS_TO, HAS_TO_FROM |
| Constructor Summary | |
|---|---|
ValuesConfigBean(Class c)
Construct a ValuesConfigBean object for a certain class. |
|
| Method Summary | |
|---|---|
Object |
checkArgument(Object o)
Verify that a object is of the type recognized by this class. |
void |
fromObject(Object o)
Transfer values from a object into this "value holder". |
String[] |
getAvailable()
Get list of available configurations. |
int |
getCapabilities()
Determine what the ValuesHolder
implementation supports. |
String |
getDefaultName()
Get the default name for the ValuesConfig
implementation. |
Class |
getObjectClass()
Get the Class which the "values holder" is
designed to work with. |
void |
load(String name)
Attempts to load a configuration from the specified name. |
void |
remove(String name)
Remove a configuration from existance. |
void |
save(String name)
Saves the current configuration in a manner that it can be loaded in the future. |
void |
toObject(Object o)
Transfer values from this "value holder" into a object. |
String |
toString()
Get a simple string representation of the ValuesConfig implementation. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
Method[] _SetMethods
Method[] _GetMethods
| Constructor Detail |
|---|
public ValuesConfigBean(Class c)
ValuesConfigBean object for a certain class.
c - Class to of bean to handle configuration of - MUST not
be null, and must support the Class.newInstance() method.
IllegalArgumentException - If the Class you pass is invalid (we were unable to create a
Object instance).| Method Detail |
|---|
public int getCapabilities()
ValuesHolder
implementation supports.
This method returns HAS_TO_FROM
indicating that the ValuesHolder interface
is fully supported. This means that values can be transferred in
both directions - from the object to the properties configuration
and from the properties configuration to the object. Any derived
class which does not support either of these operations, will
need to override this method and indicate what they do
support.
getCapabilities in interface ValuesHolderValuesHolder.HAS_TO_FROM.ValuesHolder.getCapabilities()public final String getDefaultName()
ValuesConfig
implementation.
Sometimes, one doesn't want to worry about choosing between
available configurations for a object - they simply want to work
with the 'default' configuration - or a application may only need
one configuration. In these cases, one can simply use this method
to retrieve the name for the 'default' configuration and then use
this name with the save and load
methods.
Please note that the name returned does NOT guarantee
existance of the configuration (in other words a load of the default name might fail) - someone must save a
configuration under the default name before it can be loaded.
getDefaultName in interface ValuesConfigsave and load methods.public final String[] getAvailable()
This method returns the "simple" names of ALL of the currently available configuration files that are available for loading.
getAvailable in interface ValuesConfig
public final void save(String name)
throws IOException,
ConfigException
loaded in the future.
This method attempts to permanently save the state of the "values holder" in a manner that it can be loaded in the future.
save in interface ValuesConfigname - Name or key which configuration is to be saved under
(typically in a simple form like: "liveoak" - don't assume a
file system). Must not be null.
ConfigException - If there is a problem with the configuration
IOException - If there is a problem trying to write the configuration to its
final location.load(java.lang.String)
public final void load(String name)
throws IOException,
ConfigException
This method attempts to load the values back from a
configuration which had previously been saved.
load in interface ValuesConfigname - Name or key which configuration is to be loaded (typically in
a simple form like: "liveoak" - don't assume a file
system). Must not be null.
ConfigException - If there is a problem with the configuration
IOException - If there is a problem trying to read the configuration from
the system/network.
public final void remove(String name)
throws IllegalArgumentException,
IOException
This method attempts to delete the configuration specified.
remove in interface ValuesConfigname - Name of the configuration to be removed - must not be null.
IllegalArgumentException - If there is a problem removing the configuration (didn't
exist, insufficient permissions, etc).
IOException - If there is a problem removing the configuration (didn't
exist, insufficient permissions, etc).
public final Object checkArgument(Object o)
throws NullPointerException,
IllegalArgumentException
This method is typically used by the toObject and fromObject methods. It verifies
that the object is of the proper type (and throws an exception if
not).
NullPointerException - If you pass null.
IllegalArgumentException - If the object you pass is not a instance of the object class supported.public String toString()
toString in class Objectpublic Class getObjectClass()
Class which the "values holder" is
designed to work with.
This method returns the Class which the "values
holder" is designed to work with. All objects which are passed to
the toObject or fromObject
methods MUST be instances of this class.
getObjectClass in interface ValuesHolder
public void toObject(Object o)
throws IllegalStateException,
IllegalArgumentException,
UnsupportedOperationException,
NullPointerException
This method restores the state information by invoking all of the "set" methods which have a corresponding "get" or "is" method in your bean class. Its possible that we will only get a partial transfer (an exception will be thrown in this condition) if one of the underlying "is", "get" or "set" methods we invoke during the transfer fails on us.
toObject in interface ValuesHolderNullPointerException - If you pass null.
IllegalArgumentException - If the object passed is not the proper type.
IllegalStateException - If the "values holder" isn't in the proper state to do the
transfer.
UnsupportedOperationException - If the "value holder" doesn't implement the setting of values
in the associated objects.fromObject(java.lang.Object)
public void fromObject(Object o)
throws IllegalStateException,
IllegalArgumentException,
UnsupportedOperationException,
NullPointerException
This method saves the state information from all of the "get" and "is" methods of the bean class that have a corresponding "set" method. Its possible that we will only get a partial transfer (an exception will be thrown in this condition) if one of the underlying "is", "get" or "set" methods we invoke during the transfer fails on us.
fromObject in interface ValuesHolderNullPointerException - If you pass null.
IllegalArgumentException - If the object passed is not the proper type.
IllegalStateException - If the object isn't in the proper state to do the
transfer into this "values holder".
UnsupportedOperationException - If the "value holder" doesn't implement the getting of values
from associated objects.toObject(java.lang.Object)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||