KickJava   Java API By Example, From Geeks To Geeks.

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


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.regex.*;
22 import java.util.logging.Logger JavaDoc;
23 import java.util.logging.Level JavaDoc;
24
25 import sync4j.framework.logging.Sync4jLogger;
26 import sync4j.framework.logging.Sync4jLoggerName;
27 import sync4j.framework.config.Configuration;
28 import sync4j.framework.tools.PatternPair;
29 import sync4j.framework.tools.beans.LazyInitBean;
30 import sync4j.framework.tools.beans.BeanInitializationException;
31 import sync4j.framework.core.dm.ddf.DevInfo;
32 import sync4j.framework.engine.dm.DeviceDMState;
33 import sync4j.framework.engine.dm.ManagementProcessor;
34 import sync4j.framework.server.dm.ProcessorSelector;
35
36 /**
37  * This is a simple ProcessorSelector implementation that associates management
38  * processors to sets of device identifiers. It is configured with an array of
39  * associations {regexp}-{management_processor}, where {regexp} is a regular
40  * expression interpreted by the JDK package java.util.regex used to match the
41  * device id and {management_processor} is a server side bean configuration
42  * path. If no device id matches any of the given regexp, a default processor
43  * will be returned; otherwise the first match is returned.
44  *
45  * @author Stefano Fornari @ Funambol
46  *
47  * @version $Id: DeviceIdProcessorSelector.java,v 1.1 2005/05/16 17:32:56 nichele Exp $
48  */

49 public class DeviceIdProcessorSelector
50 implements ProcessorSelector, LazyInitBean, java.io.Serializable JavaDoc {
51     // ---------------------------------------------------------- Protected data
52

53     /**
54      * Logger
55      */

56     protected static final Logger JavaDoc log = Sync4jLogger.getLogger(Sync4jLoggerName.HANDLER);
57
58     // ------------------------------------------------------------ Private data
59

60     private Pattern[] regexps;
61
62     // -------------------------------------------------------------- Properties
63

64     /**
65      * The pattern-pairs used to metch device ids
66      */

67     private PatternPair[] patterns;
68
69     /**
70      * Sets patterns
71      *
72      * @param patterns the new patterns
73      */

74     public void setPatterns(PatternPair[] patterns) {
75         this.patterns = patterns;
76     }
77
78     /**
79      * Gets patterns
80      *
81      * @return the patterns property
82      */

83     public PatternPair[] getPatterns() {
84         return patterns;
85     }
86
87     /**
88      * The default processor server bean name
89      */

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

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

106     public String JavaDoc getDefaultProcessor() {
107         return this.defaultProcessor;
108     }
109
110     // ------------------------------------------------------------ Constructors
111

112     // ------------------------------------------------------- ProcessorSelector
113

114     /**
115      * @param sessionId the management session id: ignored
116      * @param devInfo the device info
117      *
118      * @see ProcessorSelector
119      */

120     public ManagementProcessor getProcessor(DeviceDMState dms, DevInfo devInfo) {
121         String JavaDoc beanName = defaultProcessor;
122
123         String JavaDoc device = devInfo.getDevId();
124
125         Matcher m;
126         for (int i=0; i<regexps.length; ++i) {
127             m = regexps[i].matcher(device);
128
129             if (m.matches()) {
130                 beanName = patterns[i].processor;
131                 break;
132             }
133         }
134
135         ManagementProcessor processor = null;
136         try {
137             processor = (ManagementProcessor)
138                         Configuration.getConfiguration().getBeanInstanceByName(beanName);
139         } catch (Exception JavaDoc e) {
140             if (log.isLoggable(Level.SEVERE)) {
141                 log.severe( "Error instantiating the management processor"
142                           + beanName
143                           + ": "
144                           + e.getMessage()
145                           );
146             }
147             log.throwing(getClass().getName(), "getProcessor", e);
148         }
149
150         return processor;
151     }
152
153     // ------------------------------------------------------------ LazyInitBean
154

155     /**
156      * During bean initialization all the given regular expressions are compiled.
157      * If there are errors, a BeanInitializationException is thrown.
158      *
159      * @throws BeanInitializationException if one of the patterns cannot be compiled
160      */

161     public void init() throws BeanInitializationException {
162         if ((patterns == null) || (patterns.length == 0)) {
163             regexps = new Pattern[0];
164             return;
165         }
166
167         regexps = new Pattern[patterns.length];
168         for (int i=0; i<patterns.length; ++i) {
169             try {
170                 regexps[i] = Pattern.compile(patterns[i].pattern);
171             } catch (Exception JavaDoc e) {
172                 if (log.isLoggable(Level.SEVERE)) {
173                     log.severe( "Error compiling pattern '"
174                               + patterns[i].pattern
175                               + "': "
176                               + e.getMessage()
177                               );
178                 }
179                 throw new BeanInitializationException(
180                     "Error compiling pattern '" + patterns[i].pattern + "'", e
181                 );
182             }
183         }
184     }
185 }
Popular Tags