attributes basic attributes are great tool for describing your services to clients and users....

31
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);

Post on 22-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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);

Page 2: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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 .

Page 3: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 4: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

The best way to think of Entries is as collections of Java objects.

IntegerString

Data

Version Number

Last Changed By

Last Changed At

Page 5: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 6: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

An Entry before and after serialization

Page 7: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 8: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 9: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 10: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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

Page 11: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 12: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 13: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.entrynet.jini.lookup.entry package.

Page 14: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

Attribute purpose

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

All this attributes are NOT ServiceControlled!!!

Page 15: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

Attribute purpose

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

All this attributes are ServiceControlled !!!

Page 16: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 17: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

Capacity.java - An attribute examplepackage corejini ;import net.jini.entry.AbstractEntry;Import net.jini.lookup.entry.ServiceControlled

Public class Capacity extends AbstractEntry implements 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);

} }

Page 18: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 19: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 20: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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;

}

Page 21: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 22: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 23: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

EntryBean interface

Package net.jini.lookup.entry;

Public interface EntryBean { public void makeLink(Entry e);

public Entry followLink();

}

Page 24: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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.

Page 25: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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

Page 26: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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

Page 27: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

// 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

Page 28: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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

Page 29: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

// 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

Page 30: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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

Page 31: Attributes Basic Attributes are great tool for describing your services to clients and users. Attributes give you powerful ways to search for particular

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