View Javadoc
1   package org.apache.fulcrum.intake;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.beans.IntrospectionException;
23  import java.lang.reflect.Method;
24  
25  import org.apache.fulcrum.intake.model.Group;
26  
27  /**
28   * This is a Facade class for IntakeService.
29   *
30   * This class provides static methods that call related methods of the
31   * implementation of the IntakeService used by the System, according to the
32   * settings in your intake.xml file.
33   *
34   * Note: How should a facade class work? It seems to me that maybe it should
35   * only have a hook into the Avalon Component Manager somehow?
36   *
37   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
38   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
39   * @version $Id$
40   */
41  public class IntakeServiceFacade
42  {
43      /** Static instance of the intakeService. */
44      private static IntakeService intakeService;
45  
46      /**
47       * Return whether the intake service has been initialized.
48       * @return true if the service has been initialized
49       */
50      public static boolean isInitialized()
51      {
52          return intakeService != null;
53      }
54  
55      /**
56       * Gets an instance of a named group either from the pool or by calling the
57       * Factory Service if the pool is empty.
58       *
59       * @param groupName
60       *            the name of the group.
61       * @return a Group instance.
62       * @throws IntakeException
63       *             if recycling fails.
64       */
65      public static Group getGroup(String groupName) throws IntakeException
66      {
67          if (groupName == null)
68          {
69              throw new IntakeException("IntakeServiceFacade.getGroup(groupName) is null");
70          }
71          return getService().getGroup(groupName);
72      }
73  
74      /**
75       * Puts a group back to the pool.
76       *
77       * @param instance
78       *            the object instance to recycle.
79       * @throws IntakeException
80       *             A non existant group was passed
81       */
82      public static void releaseGroup(Group instance) throws IntakeException
83      {
84          getService().releaseGroup(instance);
85      }
86  
87      /**
88       * Gets the current size of the pool for a named group.
89       *
90       * @param groupName
91       *            the name of the group.
92       * @return the current pool size
93       * @throws IntakeException
94       *             A non-existent group was passed
95       */
96      public static int getSize(String groupName) throws IntakeException
97      {
98          return getService().getSize(groupName);
99      }
100 
101     /**
102      * Names of all the defined groups.
103      *
104      * @return array of names.
105      */
106     public static String[] getGroupNames()
107     {
108         return getService().getGroupNames();
109     }
110 
111     /**
112      * Gets the key (usually a short identifier) for a group.
113      *
114      * @param groupName
115      *            the name of the group.
116      * @return the the key.
117      */
118     public static String getGroupKey(String groupName)
119     {
120         return getService().getGroupKey(groupName);
121     }
122 
123     /**
124      * Gets the group name given its key.
125      *
126      * @param groupKey
127      *            the key.
128      * @return groupName the name of the group.
129      */
130     public static String getGroupName(String groupKey)
131     {
132         return getService().getGroupName(groupKey);
133     }
134 
135     /**
136      * Gets the Method that can be used to set a property.
137      *
138      * @param className
139      *            the name of the object.
140      * @param propName
141      *            the name of the property.
142      * @return the setter.
143      * @throws ClassNotFoundException if the class specified could not be loaded
144      * @throws IntrospectionException if the property setter could not be called
145      */
146     public static Method getFieldSetter(String className, String propName) throws IntrospectionException, ClassNotFoundException
147     {
148         return getService().getFieldSetter(className, propName);
149     }
150 
151     /**
152      * Gets the Method that can be used to get a property value.
153      *
154      * @param className
155      *            the name of the object.
156      * @param propName
157      *            the name of the property.
158      * @return the getter.
159      * @throws ClassNotFoundException if the class specified could not be loaded
160      * @throws IntrospectionException if the property getter could not be called
161      */
162     public static Method getFieldGetter(String className, String propName) throws IntrospectionException, ClassNotFoundException
163     {
164         return getService().getFieldGetter(className, propName);
165     }
166 
167     /**
168      * Utility method for accessing the service implementation
169      *
170      * @return a IntakeService implementation instance
171      */
172     private static IntakeService getService()
173     {
174         return intakeService;
175     }
176 
177     public static void setIntakeService(IntakeService service)
178     {
179         intakeService = service;
180     }
181 
182 }