Download Attributes Basic

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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);
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.UnuseableEntryExeption.
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 MarshalledObjects.
• Comparison of two fields calls the equals()
method on MarshalledObject .
• 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 are 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.ServiceControlled 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 ServiceControlled !!!
Attribute
• ServiceInfo
purpose
• Information about the service
manufacturer, model
name,serial number and so on.
• ServiceType
• Describe a service.
• Status
• Information on the current
operating state of a service.
All this attributes are ServiceControlled !!!
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.
Capacity.java - An attribute example
package 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);
}}
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.
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.
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;
}
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.
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.
EntryBean interface
Package net.jini.lookup.entry;
Public interface EntryBean {
public void makeLink(Entry e);
public Entry followLink();
}
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.
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
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
// 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;
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
// 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
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
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