View Javadoc

1   package org.codehaus.xfire.service;
2   
3   import java.lang.reflect.Method;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import javax.xml.namespace.QName;
11  
12  /***
13   * Represents an description of a service. A service consists of a number of <code>OperationInfo</code> objects, a
14   * qualified name, and a service class.
15   *
16   * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
17   * @see OperationInfo
18   */
19  public class ServiceInfo
20          implements Visitable
21  {
22      private QName name;
23      private Map operations = new HashMap();
24      private Map opsByAction = new HashMap();
25      private Class serviceClass;
26  
27      /***
28       * Initializes a new instance of the <code>ServiceInfo</code> class with the given qualified name and service
29       * class.
30       *
31       * @param name         the qualified name.
32       * @param serviceClass the service class.
33       */
34      public ServiceInfo(QName name, Class serviceClass)
35      {
36          this.name = name;
37          this.serviceClass = serviceClass;
38      }
39  
40      /***
41       * Acceps the given visitor. Iterates over all operation infos.
42       *
43       * @param visitor the visitor.
44       */
45      public void accept(Visitor visitor)
46      {
47          visitor.startService(this);
48          for (Iterator iterator = operations.values().iterator(); iterator.hasNext();)
49          {
50              OperationInfo operationInfo = (OperationInfo) iterator.next();
51              operationInfo.accept(visitor);
52          }
53          visitor.endService(this);
54      }
55  
56      /***
57       * Adds an operation to this service.
58       *
59       * @param name the qualified name of the operation.
60       * @return the operation.
61       */
62      public OperationInfo addOperation(String name, Method method)
63      {
64          if ((name == null) || (name.length() == 0))
65          {
66              throw new IllegalArgumentException("Invalid name [" + name + "]");
67          }
68          if (operations.containsKey(name))
69          {
70              throw new IllegalArgumentException("An operation with name [" + name + "] already exists in this service");
71          }
72  
73          OperationInfo operation = new OperationInfo(name, method, this);
74          addOperation(operation);
75          return operation;
76      }
77  
78      /***
79       * Adds an operation to this service.
80       *
81       * @param operation the operation.
82       */
83      void addOperation(OperationInfo operation)
84      {
85          operations.put(operation.getName(), operation);
86      }
87  
88      /***
89       * Returns the qualified name of the service descriptor.
90       *
91       * @return the qualified name.
92       */
93      public QName getName()
94      {
95          return name;
96      }
97  
98      /***
99       * Sets the qualified name of the service descriptor.
100      *
101      * @param name the new qualified name.
102      */
103     public void setName(QName name)
104     {
105         this.name = name;
106     }
107 
108     /***
109      * Returns the operation info with the given name, if found.
110      *
111      * @param name the name.
112      * @return the operation; or <code>null</code> if not found.
113      */
114     public OperationInfo getOperation(String name)
115     {
116         return (OperationInfo) operations.get(name);
117     }
118 
119     /***
120      * Returns the operation info with the given action, if found.
121      *
122      * @param name the name.
123      * @return the operation; or <code>null</code> if not found.
124      */
125     public OperationInfo getOperationByAction(String name)
126     {
127         for (Iterator itr = operations.values().iterator(); itr.hasNext();)
128         {
129             OperationInfo op = (OperationInfo) itr.next();
130             if (op.getAction() != null && op.getAction().equals(name))
131             {
132                 return op;
133             }
134         }
135         
136         if (!name.equals("*"))
137         {
138             return getOperationByAction("*");
139         }
140 
141         return null;
142     }
143     
144     /***
145      * Returns all operations for this service.
146      *
147      * @return all operations.
148      */
149     public Collection getOperations()
150     {
151         return Collections.unmodifiableCollection(operations.values());
152     }
153 
154     /***
155      * Returns the service class of the service descriptor.
156      *
157      * @return
158      */
159     public Class getServiceClass()
160     {
161         return serviceClass;
162     }
163 
164     /***
165      * Removes an operation from this service.
166      *
167      * @param name the operation name.
168      */
169     public void removeOperation(String name)
170     {
171         operations.remove(name);
172     }
173 }