KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > engine > dm > OperationProcessorSelector


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.engine.dm;
20
21 import java.util.logging.Logger JavaDoc;
22 import java.util.logging.Level JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25
26 import sync4j.framework.tools.beans.*;
27
28 import sync4j.framework.logging.Sync4jLogger;
29 import sync4j.framework.logging.Sync4jLoggerName;
30 import sync4j.framework.config.Configuration;
31 import sync4j.framework.config.ConfigurationConstants;
32 import sync4j.framework.server.dm.ProcessorSelector;
33 import sync4j.framework.engine.dm.ManagementProcessor;
34 import sync4j.framework.engine.dm.DeviceDMState;
35 import sync4j.framework.core.dm.ddf.DevInfo;
36
37 /**
38  * This is an implementation of ProcessorSelector that selects a management
39  * processor looking up the management operation the management session was
40  * meant for.
41  * <p>
42  * If no processor is found for the given session id, the processor specified by
43  * <i>defaultProcessor</i> is used.
44  * <p>
45  * If a session is found, the processor name is computed as follows:
46  * <br>
47  * <i>namePrefix</i> + <i>operation</i> + <i>namePostfix</i>
48  * <br>
49  * Where <i>namePrefix</i> and <i>namePostfix</i> are the two homonymous properties.
50  *
51  * @author Stefano Fornari @ Funambol
52  *
53  * @version $Id: OperationProcessorSelector.java,v 1.1 2005/05/16 17:32:56 nichele Exp $
54  */

55 public class OperationProcessorSelector
56 implements ProcessorSelector, LazyInitBean, ConfigurationConstants {
57
58     // ------------------------------------------------------------ Private data
59

60     private transient Logger JavaDoc log;
61
62     // -------------------------------------------------------------- Properties
63

64     /**
65      * The default processor server bean name
66      */

67     private String JavaDoc defaultProcessor;
68
69     /**
70      * Sets defaultProcessor
71      *
72      * @param defaultProcessor the new default processor name
73      */

74     public void setDefaultProcessor(String JavaDoc defaultProcessor) {
75         this.defaultProcessor = defaultProcessor;
76     }
77
78     /**
79      * Returns defaultProcessor
80      *
81      * @return defaultProcessor property value
82      */

83     public String JavaDoc getDefaultProcessor() {
84         return this.defaultProcessor;
85     }
86
87     /**
88      * The error processor server bean name
89      */

90     private String JavaDoc errorProcessor;
91
92     /**
93      * Sets errorProcessor
94      *
95      * @param errorProcessor the new error processor name
96      */

97     public void setErrorProcessor(String JavaDoc errorProcessor) {
98         this.errorProcessor = errorProcessor;
99     }
100
101     /**
102      * Returns errorProcessor
103      *
104      * @return errorProcessor property value
105      */

106     public String JavaDoc getErrorProcessor() {
107         return this.errorProcessor;
108     }
109
110     /**
111      * Processor name prefix (to be prepended to the operation name).
112      */

113     private String JavaDoc namePrefix;
114
115     /**
116      * Sets namePrefix
117      *
118      * @param namePrefix the new processor name prefix
119      */

120     public void setNamePrefix(String JavaDoc namePrefix) {
121         this.namePrefix = namePrefix;
122     }
123
124     /**
125      * Returns namePrefix
126      *
127      * @return namePrefix property value
128      */

129     public String JavaDoc getNamePrefix() {
130         return namePrefix;
131     }
132
133     /**
134      * Processor name postfix (to be appended to the operation name).
135      */

136     private String JavaDoc namePostfix;
137
138     /**
139      * Sets namePostfix
140      *
141      * @param namePostfix the new processor name postfix
142      */

143     public void setNamePostfix(String JavaDoc namePostfix) {
144         this.namePostfix = namePostfix;
145     }
146
147     /**
148      * Returns namePostfix
149      *
150      * @return namePostfix property value
151      */

152     public String JavaDoc getNamePostfix() {
153         return namePostfix;
154     }
155
156
157     // ------------------------------------------------------------ Constructors
158

159     /**
160      * Creates a new instance of OperationProcessorSelector
161      */

162     public OperationProcessorSelector() {
163         log = Sync4jLogger.getLogger(Sync4jLoggerName.ENGINE);
164     }
165
166     /**
167      *
168      * @see sync4j.framework.server.dm.ProcessorSelector
169      */

170     public ManagementProcessor getProcessor(DeviceDMState dms, DevInfo devInfo) {
171         String JavaDoc processorName = null;
172
173         if (log.isLoggable(Level.FINE)) {
174             log.fine("dms: " + dms);
175         }
176
177         if (dms.state == DeviceDMState.STATE_ERROR) {
178             processorName = errorProcessor;
179         } else {
180             if (StringUtils.isEmpty(dms.operation)) {
181                 processorName = defaultProcessor;
182             } else {
183                 processorName = namePrefix + dms.operation + namePostfix;
184             }
185         }
186
187         if (log.isLoggable(Level.FINE)) {
188             log.fine("Selected processor: " + processorName);
189         }
190
191         try {
192             return (ManagementProcessor)
193                    Configuration.getConfiguration().getBeanInstanceByName(processorName);
194         } catch (Exception JavaDoc e){
195             if (log.isLoggable(Level.SEVERE)) {
196                 log.severe("Error creating the management processor: " + e.getMessage());
197                 log.throwing(getClass().getName(), "getProcessor", e);
198             }
199         }
200
201         return null;
202     }
203
204     /**
205      * Checks if the parameters are ok. If not, it throws an exception. <br>
206      * If namePrefix and/or namePostfix are null, an empty string is assumed.
207      * If defaultProcessor is not given, an exception is thrown.
208      *
209      * @throws BeanInitializationException in case of initialization errors
210      */

211     public void init() throws BeanInitializationException {
212         if (StringUtils.isEmpty(defaultProcessor)) {
213             throw new BeanInitializationException("Missing mandatory parameter defaultProcessor");
214         }
215
216         if (StringUtils.isEmpty(namePrefix)) {
217             namePrefix = "";
218         }
219
220         if (StringUtils.isEmpty(namePostfix)) {
221             namePostfix = "";
222         }
223     }
224
225     // --------------------------------------------------------- Private methods
226

227 }
Popular Tags