View Javadoc

1   /*
2    * 
3    * Copyright 2005 Sun Microsystems, Inc.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   * 
17   * Note: Changes by the Jgroup team are marked ** GREG **.
18   * 
19   */
20  package com.sun.jini.reggie;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.rmi.MarshalException;
25  import java.rmi.NoSuchObjectException;
26  import java.rmi.Remote;
27  import java.rmi.RemoteException;
28  import java.rmi.server.RemoteObject;
29  import java.util.ArrayList;
30  import java.util.logging.Level;
31  import java.util.logging.Logger;
32  
33  import jgroup.jini.GroupEntry;
34  import net.jini.core.lookup.ServiceID;
35  import net.jini.core.lookup.ServiceItem;
36  import net.jini.security.Security;
37  
38  import com.sun.jini.action.GetBooleanAction;
39  import com.sun.jini.logging.Levels;
40  import com.sun.jini.proxy.MarshalledWrapper;
41  
42  /**
43   * An Item contains the fields of a ServiceItem packaged up for
44   * transmission between client-side proxies and the registrar server.
45   * Instances are never visible to clients, they are private to the
46   * communication between the proxies and the server.
47   * <p>
48   * This class only has a bare minimum of methods, to minimize
49   * the amount of code downloaded into clients.
50   *
51   * @author Sun Microsystems, Inc.
52   *
53   */
54  class Item implements Serializable, Cloneable
55  {
56  
57    private static final long serialVersionUID = 2L;
58  
59    /** Logger for Reggie. */
60    private static final Logger logger = Logger.getLogger("com.sun.jini.reggie");
61  
62    /**
63     * Flag to enable JRMP impl-to-stub replacement during marshalling of
64     * service proxy.
65     */
66    private static final boolean enableImplToStubReplacement;
67    static {
68      Boolean b;
69      try {
70        b = (Boolean) Security.doPrivileged(new GetBooleanAction(
71            "com.sun.jini.reggie.enableImplToStubReplacement"));
72      } catch (SecurityException e) {
73        logger.log(Levels.HANDLED, "failed to read system property", e);
74        b = Boolean.FALSE;
75      }
76      enableImplToStubReplacement = b.booleanValue();
77    }
78  
79    /**
80     * ServiceItem.serviceID.
81     *
82     * @serial
83     */
84    public ServiceID serviceID;
85  
86    /**
87     * The Class of ServiceItem.service converted to ServiceType.
88     *
89     * @serial
90     */
91    public ServiceType serviceType;
92  
93    /**
94     * The codebase of the service object.
95     *
96     * @serial
97     */
98    public String codebase;
99  
100   /**
101    * ServiceItem.service as a MarshalledWrapper.
102    *
103    * @serial
104    */
105   public MarshalledWrapper service;
106 
107   /**
108    * ServiceItem.attributeSets converted to EntryReps.
109    *
110    * @serial
111    */
112   public EntryRep[] attributeSets;
113   
114   /**
115    * Group identifier (if any) ** GREG **
116    */
117   public String gid;
118 
119   /**
120    * Converts a ServiceItem to an Item.  Any exception that results
121    * is bundled up into a MarshalException.
122    */
123   public Item(ServiceItem item) throws RemoteException
124   {
125     Object svc = item.service;
126     if (enableImplToStubReplacement && svc instanceof Remote) {
127       try {
128         svc = RemoteObject.toStub((Remote) svc);
129         if (logger.isLoggable(Level.FINER)) {
130           logger
131               .log(Level.FINER, "replacing {0} with {1}", new Object[] { item.service, svc });
132         }
133       } catch (NoSuchObjectException e) {
134       }
135     }
136     serviceID = item.serviceID;
137     ServiceTypeBase stb = ClassMapper.toServiceTypeBase(svc.getClass());
138     serviceType = stb.type;
139     codebase = stb.codebase;
140     try {
141       service = new MarshalledWrapper(svc);
142     } catch (IOException e) {
143       throw new MarshalException("error marshalling arguments", e);
144     }
145     attributeSets = EntryRep.toEntryRep(item.attributeSets, true);
146     
147     // Search for a group name ** GREG **
148     for (int i=0; i < item.attributeSets.length && gid == null; i++) {
149        if (item.attributeSets[i] instanceof GroupEntry) {
150           GroupEntry groupEntry = (GroupEntry) item.attributeSets[i];
151           gid = groupEntry.groupName;
152        }
153     }
154   }
155 
156   /**
157    * Convert back to a ServiceItem.  If the service object cannot be
158    * constructed, it is set to null.  If an Entry cannot be constructed,
159    * it is set to null.  If a field of an Entry cannot be unmarshalled,
160    * it is set to null.
161    */
162   public ServiceItem get()
163   {
164     Object obj = null;
165     try {
166       obj = service.get();
167     } catch (Throwable e) {
168       RegistrarProxy.handleException(e);
169     }
170     return new ServiceItem(serviceID, obj, EntryRep.toEntry(attributeSets));
171   }
172 
173   /**
174    * Deep clone.  This is really only needed in the server,
175    * but it's very convenient to have here.
176    */
177   public Object clone()
178   {
179     try {
180       Item item = (Item) super.clone();
181       EntryRep[] attrSets = (EntryRep[]) item.attributeSets.clone();
182       for (int i = attrSets.length; --i >= 0;) {
183         attrSets[i] = (EntryRep) attrSets[i].clone();
184       }
185       item.attributeSets = attrSets;
186       return item;
187     } catch (CloneNotSupportedException e) {
188       throw new InternalError();
189     }
190   }
191 
192   /**
193    * Converts an ArrayList of Item to an array of ServiceItem.
194    */
195   public static ServiceItem[] toServiceItem(ArrayList reps)
196   {
197     ServiceItem[] items = null;
198     if (reps != null) {
199       items = new ServiceItem[reps.size()];
200       for (int i = items.length; --i >= 0;) {
201         items[i] = ((Item) reps.get(i)).get();
202       }
203     }
204     return items;
205   }
206 }