Attributes Basic Attributes are great tool for describing
Attributes Basic • Attributes are great tool for describing your services to clients and users. • Attributes give you powerful ways to search for particular services that you need to use. • Last week example : Service. Item item = new Service. Item (ID, create. Proxy, null);
? What Are Attributes • Attributes are Java objects that are attached to service proxies. • Clients can search lookup services by looking for certain attribute pattern , and they can download the attribute. • Attributes are used to associate extra descriptive information with a service. • Important : Every Java object that is used as an attribute must implement the net. jini. core. entry. Entry interface.
The Special Semantics of Attributes • You can think of attribute as serializable Java object. • Searchable attribute is a collection of its data field. All the follow are ignored : – Methods – Static – Nonpublic – Transient – Final – Primitive • So only references to other objects within an attribute are considered for searching.
The best way to think of Entries is as collections of Java objects. Version Number Integer Last Changed By Last Changed At Data String
Entry Semantic • Entry objects are serializable and as such must have an empty constructor !! • All the field of a given Entry are serialized separately and independently from one another. – Philosophical reason. – Practical reason.
An Entry before and after serialization
Semantic - Summery • Only the public , nonstatic , nonfinal , nontransient object references “matter” for search. • It is possible to add code , data and methods to the attributes. • Net. jini. core. entry. Unuseable. Entry. Exeption.
Search using Attributes • Simple , Quick , Effective. • What is the criterion to search by? – Type based search : looking according to the class. – Content base search : looking according to the data.
? How does the search work • Attribute search uses the notion of attribute templates. • Template is itself an attribute. • The template is compared to the attribute. • For a match they must be from the same class or subclass. • The data fields in the template that are non-null must match exactly the corresponding data fields in the attribute.
• In the lookup service , the fields of templates and attributes are stored in their serialized forms as Marshalled. Objects. • Comparison of two fields calls the equals() method on Marshalled. Object. • Disadvantage : no option to search for a null field.
? Who changes Attributes • What happens when the status of a service change? • Service – sometimes its enough. • Human –there are some changes that the service is not aware of. • There a wealth of human-attachable attributes that one might want to add.
Create and add new attributes • Jini provides a convention that attribute writers can use to describe which type of attribute they are creating. – An attribute that changes only by the service: should implement the net. jini. lookup. entry. Service. Controlled interface. – An attribute that doesn’t implement this interface saying that it is freely writable.
? How do clients change attributes • The service should “allow” them to add attributes to their proxies. • Most services in jini should provide an “administration” interface : – Start or stop service. – Change the set of groups it joins. – Change the set of explicit lookup services it registers itself with. • Jini provide a number of “standard” Attributes that defined in the net. jini. lookup. entry package.
Attribute • Address • Comment • Location • Name purpose • Information on the geographical location of the service. • Free-form string comment on a service. • Information on the location of the service. • A name for the service that will be seen by the users. All this attributes are NOT Service. Controlled !!!
Attribute • Service. Info purpose • Information about the service manufacturer, model name, serial number and so on. • Service. Type • Describe a service. • Status • Information on the current operating state of a service. All this attributes are Service. Controlled !!!
Writing new Attributes • Create a class that implements the Entry interface. – Entry is subinterface of Serializable. • All object reference must be serialized independently from one another. • If you are writing a simple attribute that doesn’t inherit any class , you can use the jini Abstract. Entry as your attribute’s superclass. – This class lives in net. jini. entry and includes the following methods: • Equals() • Hash. Code() • to. String() • Human readability.
Capacity. java - An attribute example package corejini ; import net. jini. entry. Abstract. Entry; Import net. jini. lookup. entry. Service. Controlled Public class Capacity extends Abstract. Entry implements Service. Controlled { Public Integer max. Capacity; Public Integer in. Use; Public Integer free. Space; Public Capacity(){ max. Capacity = new Integer(0); in. Use = new Integer(0); free. Space = new Integer(0); }//constructor public Capacity(int max , int used){ max. Capacity = new Integer(max); in. Use = new Integer(used); free. Space = new Integer(max-used); }}
Attributes and Beans • Bean – special object that follow certain design patterns. • The java. Beans introspector class has the intelligence to look at a class that follow the bean conventions , and report on how to interact with the bean. • Bean. Info class provides details on the fields and methods of the bean.
Using Entry Beans to map Entries to Beans • Entry beans – beans that correspond to particular Entry classes. – Entry bean provide get and set methods for every data member. – All Entry bean implement the Entry. Bean interface. • To create Entry bean use the net. jini. lookup. entry. Entry. Beans class.
: Class method package net. jini. lookup. entry Public class Entry. Beans { public static Entry. Bean create. Bean(Entry ent) throws Class. Not. Found. Exeption, IOExeption; public static class get. Bean. Class(class c) throws Class. Not. Found. Exeption; }
How are Entry beans found • For an Entry with a given class name , say foo, the Entry. Beans class tries to find a class cold Foo. Bean. • It tries by calling first the class loader that originally loaded the Entry , and then by calling its own default class loader.
The Entry Bean Class • If you wish to create a new attribute type and want to make a new bean to get along with it you must take the following steps: – Create your bean class by implementing Entry. Bean. – Name your bean class by taking the name of your Entry’s class and appending “Bean” to it. – Make sure your bean has a public no-argument constructor and implements serializable. – Provide get and set for every data member.
Entry. Bean interface Package net. jini. lookup. entry; Public interface Entry. Bean { public void make. Link(Entry e); public Entry follow. Link(); }
The standard Entry Beans • Jini provides a standard set of attributes , it also provides a standard set of beans to wrap those attributes. • For each of the standard Entry types , net. jini. lookup. entry package contain a corresponding Entry. Bean.
A simple Entry. Bean to allow a Capacity entry to be used via the // bean conventions. package corejini. chapter 7; import net. jini. core. entry. Entry; import net. jini. lookup. entry. Entry. Bean; import java. io. Serializable; public class Capacity. Bean implements Entry. Bean, Serializable { protected Capacity assoc = null; public Capacity. Bean() { } public Entry follow. Link() { return assoc; } public void make. Link(Entry e) { if (e != null && !(e instanceof Capacity)) { throw new Class. Cast. Exception("Expectedcorejini. chapter 7. Capacity"); } assoc = (Capacity) e; } // make. Link
public Integer get. Max. Capacity() { return assoc. max. Capacity; } public void set. Max. Capacity(Integer max. Capacity) { assoc. max. Capacity = max. Capacity; } public Integer get. In. Use() { return assoc. in. Use; } public void set. In. Use(Integer in. Use) { assoc. in. Use = in. Use; } public Integer get. Free. Space() { return assoc. free. Space; } public void set. Free. Space(Integer free. Space) { assoc. free. Space = free. Space; } }//end class
// Provides extra information about a Capacity. Bean. package corejini. chapter 7; import java. beans. Bean. Info; import java. awt. Image; import java. beans. Bean. Descriptor; import java. beans. Method. Descriptor; import java. beans. Event. Set. Descriptor; import java. beans. Property. Descriptor; public class Capacity. Bean. Info implements Bean. Info { protected Bean. Descriptor bean. Desc = null; public Capacity. Bean. Info() { } public Bean. Info[] get. Additional. Bean. Info() { return null; } public Bean. Descriptor get. Bean. Descriptor() { if (bean. Desc == null) { bean. Desc = new Bean. Descriptor(corejini. chapter 7. Capacity. Bean. class, null); bean. Desc. set. Short. Description("This Jini attribute shows " + "the capacity of a storage " + "service. "); } return bean. Desc;
public Event. Set. Descriptor[] get. Event. Set. Descriptors() { return null; } public int get. Default. Event. Index() { return -1; } public Property. Descriptor[] get. Property. Descriptors() { return null; } public int get. Default. Property. Index() { return -1; } public Image get. Icon(int param 1) { return null; } public Method. Descriptor[] get. Method. Descriptors() { return null; } }//end class
// An Entry. Bean to allow a Capacity entry to be used via the bean conventions. This version of Capacity. Bean is a GUI object. package corejini. chapter 7; import net. jini. core. entry. Entry; import net. jini. lookup. entry. Entry. Bean; import java. io. Serializable; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JProgress. Bar; import javax. swing. Box. Layout; public class Capacity. Bean extends JPanel implements Entry. Bean, Serializable { protected Capacity assoc = null; public Capacity. Bean() { super(true); set. Background(java. awt. Color. white); set. Layout(new Box. Layout(this, Box. Layout. Y_AXIS)); add(new JLabel("Not initialized")); } //constructor
public Entry follow. Link() { return assoc; } public void make. Link(Entry e) { if (e != null && !(e instanceof Capacity)) { throw new Class. Cast. Exception("Expected corejini. chapter 7. Capacity"); } assoc = (Capacity) e; init(); } protected void init() { // clear out anything lingering from a previous init() remove. All(); JLabel label = new JLabel("Max = " + get. Max. Capacity() + ", used = " + get. In. Use() + ", free = " + get. Free. Space()); JProgress. Bar slider = new JProgress. Bar(0, get. Max. Capacity(). int. Value()); slider. set. Value(get. In. Use(). int. Value()); add(slider); add(label); }//init
public Integer get. Max. Capacity() { return assoc. max. Capacity; } public void set. Max. Capacity(Integer max. Capacity) { assoc. max. Capacity = max. Capacity; } public Integer get. In. Use() { return assoc. in. Use; } public void set. In. Use(Integer in. Use) { assoc. in. Use = in. Use; } public Integer get. Free. Space() { return assoc. free. Space; } public void set. Free. Space(Integer free. Space) { assoc. free. Space = free. Space; } }//end class
- Slides: 31