1 / 31

Attributes Basic

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 : ServiceItem item = new ServiceItem

bethlewis
Download Presentation

Attributes Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 : ServiceItem item = new ServiceItem (ID,createProxy,null);

  2. 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 .

  3. 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.

  4. The best way to think of Entries is as collections of Java objects. A "service Version" Entry For content-search , an entry is the collection of its Members Version Number Integer String Last Changed By Data Last Changed At

  5. 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.

  6. An Entry before and after serialization An Entry Before Serialization Same Entry after Reconstitution

  7. 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.UnuseableEntryExeption.

  8. 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.

  9. 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.

  10. In the lookup service , the fields of templates and attributes are stored in their serialized forms as MarshalledObjects. • Comparison of two fields calls the equals() method on MarshalledObject . • Disadvantage : no option to search for a null field.

  11. 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 are a wealth of human-attachable attributes that one might want to add.

  12. 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.ServiceControlled interface. • An attribute that doesn’t implement this interface saying that it is freely writable.

  13. 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.

  14. Address Comment Location Name 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. Attribute purpose All this attributes are NOT ServiceControlled !!!

  15. ServiceInfo ServiceType Status Information about the service manufacturer, model name,serial number and so on. Describe a service. Information on the current operating state of a service. Attribute purpose All this attributes are ServiceControlled !!!

  16. 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 AbstractEntry as your attribute’s superclass. • This class lives in net.jini.entry and includes the following methods: • Equals() • HashCode() • toString() • Human readability.

  17. Capacity.java - An attribute example package corejini ; import net.jini.entry.AbstractEntry; Import net.jini.lookup.entry.ServiceControlled Public class Capacity extends AbstractEntryimplements ServiceControlled { Public Integer maxCapacity; Public Integer inUse; Public Integer freeSpace; Public Capacity(){ maxCapacity = new Integer(0); inUse = new Integer(0); freeSpace = new Integer(0); }//constructor public Capacity(int max , int used){ maxCapacity = new Integer(max); inUse = new Integer(used); freeSpace = new Integer(max-used); } }

  18. Attributes and Beans • Bean – special object that follow certain design patterns. • The javaBeans introspector class has the intelligence to look at a class that follow the bean conventions , and report on how to interact with the bean. • BeanInfo class provides details on the fields and methods of the bean.

  19. 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 EntryBean interface. • To create Entry bean use the net.jini.lookup.entry.EntryBeans class.

  20. Class method : package net.jini.lookup.entry Public class EntryBeans { public static EntryBean createBean(Entry ent) throws ClassNotFoundExeption,IOExeption; public static class getBeanClass(class c) throws ClassNotFoundExeption; }

  21. How are Entry beans found • For an Entry with a given class name , say foo, the EntryBeans class tries to find a class cold FooBean. • It tries by calling first the class loader that originally loaded the Entry , and then by calling its own default class loader.

  22. 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 EntryBean. • 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.

  23. EntryBean interface Package net.jini.lookup.entry; Public interface EntryBean { public void makeLink(Entry e); public Entry followLink(); }

  24. 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 EntryBean.

  25. A simple EntryBean to allow a Capacity entry to be used via the // bean conventions. package corejini.chapter7; import net.jini.core.entry.Entry; import net.jini.lookup.entry.EntryBean; import java.io.Serializable; public class CapacityBean implements EntryBean, Serializable { protected Capacity assoc = null; public CapacityBean() { } public Entry followLink() { return assoc; } public void makeLink(Entry e) { if (e != null && !(e instanceof Capacity)) { throw new ClassCastException("Expectedcorejini.chapter7.Capacity"); } assoc = (Capacity) e; } // makeLink

  26. public Integer getMaxCapacity() { return assoc.maxCapacity; } public void setMaxCapacity(Integer maxCapacity) { assoc.maxCapacity = maxCapacity; } public Integer getInUse() { return assoc.inUse; } public void setInUse(Integer inUse) { assoc.inUse = inUse; } public Integer getFreeSpace() { return assoc.freeSpace; } public void setFreeSpace(Integer freeSpace) { assoc.freeSpace = freeSpace; } }//end class

  27. // Provides extra information about a CapacityBean. package corejini.chapter7; import java.beans.BeanInfo;import java.awt.Image; import java.beans.BeanDescriptor; import java.beans.MethodDescriptor; import java.beans.EventSetDescriptor; import java.beans.PropertyDescriptor; public class CapacityBeanBeanInfo implements BeanInfo { protected BeanDescriptor beanDesc = null; public CapacityBeanBeanInfo() { } public BeanInfo[] getAdditionalBeanInfo() { return null; } public BeanDescriptor getBeanDescriptor() { if (beanDesc == null) { beanDesc = new BeanDescriptor(corejini.chapter7.CapacityBean.class, null); beanDesc.setShortDescription("This Jini attribute shows " + "the capacity of a storage " + "service."); } return beanDesc; }// BeanDescriptor

  28. public EventSetDescriptor[] getEventSetDescriptors() { return null; } public int getDefaultEventIndex() { return -1; } public PropertyDescriptor[] getPropertyDescriptors() { return null; } public int getDefaultPropertyIndex() { return -1; } public Image getIcon(int param1) { return null; } public MethodDescriptor[] getMethodDescriptors() { return null; } }//end class

  29. // An EntryBean to allow a Capacity entry to be used via the bean conventions. This version of CapacityBean is a GUI object . package corejini.chapter7; import net.jini.core.entry.Entry; import net.jini.lookup.entry.EntryBean; import java.io.Serializable; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.BoxLayout; public class CapacityBean extends JPanel implements EntryBean, Serializable { protected Capacity assoc = null; public CapacityBean() { super(true); setBackground(java.awt.Color.white); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(new JLabel("Not initialized")); } //constructor

  30. public Entry followLink() { return assoc; } public void makeLink(Entry e) { if (e != null && !(e instanceof Capacity)) { throw new ClassCastException("Expected corejini.chapter7.Capacity"); } assoc = (Capacity) e; init(); } protected void init() { // clear out anything lingering from a previous init() removeAll(); JLabel label = new JLabel("Max = " + getMaxCapacity() + ", used = " + getInUse() + ", free = " + getFreeSpace()); JProgressBar slider = new JProgressBar(0, getMaxCapacity().intValue()); slider.setValue(getInUse().intValue()); add(slider); add(label); }//init

  31. public Integer getMaxCapacity() { return assoc.maxCapacity; } public void setMaxCapacity(Integer maxCapacity) { assoc.maxCapacity = maxCapacity; } public Integer getInUse() { return assoc.inUse; } public void setInUse(Integer inUse) { assoc.inUse = inUse; } public Integer getFreeSpace() { return assoc.freeSpace; } public void setFreeSpace(Integer freeSpace) { assoc.freeSpace = freeSpace; } }//end class

More Related