1 18 19 package sync4j.server.engine.dm; 20 21 import java.util.regex.*; 22 import java.util.logging.Logger ; 23 import java.util.logging.Level ; 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 49 public class DeviceIdProcessorSelector 50 implements ProcessorSelector, LazyInitBean, java.io.Serializable { 51 53 56 protected static final Logger log = Sync4jLogger.getLogger(Sync4jLoggerName.HANDLER); 57 58 60 private Pattern[] regexps; 61 62 64 67 private PatternPair[] patterns; 68 69 74 public void setPatterns(PatternPair[] patterns) { 75 this.patterns = patterns; 76 } 77 78 83 public PatternPair[] getPatterns() { 84 return patterns; 85 } 86 87 90 private String defaultProcessor; 91 92 97 public void setDefaultProcessor(String defaultProcessor) { 98 this.defaultProcessor = defaultProcessor; 99 } 100 101 106 public String getDefaultProcessor() { 107 return this.defaultProcessor; 108 } 109 110 112 114 120 public ManagementProcessor getProcessor(DeviceDMState dms, DevInfo devInfo) { 121 String beanName = defaultProcessor; 122 123 String 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 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 155 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 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 |