1 40 41 package com.sun.jmx.examples.scandir; 42 43 import static com.sun.jmx.examples.scandir.ScanManager.getNextSeqNumber; 44 import static com.sun.jmx.examples.scandir.ScanDirConfigMXBean.SaveState.*; 45 import com.sun.jmx.examples.scandir.config.XmlConfigUtils; 46 import com.sun.jmx.examples.scandir.config.DirectoryScannerConfig; 47 import com.sun.jmx.examples.scandir.config.FileMatch; 48 import com.sun.jmx.examples.scandir.config.ScanManagerConfig; 49 import java.io.File ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.OutputStream ; 53 import java.util.Date ; 54 import java.util.logging.Level ; 55 import java.util.logging.Logger ; 56 import javax.management.*; 57 import javax.xml.bind.JAXBException; 58 59 103 public class ScanDirConfig extends NotificationBroadcasterSupport 104 implements ScanDirConfigMXBean, MBeanRegistration { 105 106 109 private static final Logger LOG = 110 Logger.getLogger(ScanDirConfig.class.getName()); 111 112 private final static String NOTIFICATION_PREFIX = 121 ScanManagerConfig.class.getPackage().getName(); 122 123 127 public final static String NOTIFICATION_SAVED = 128 NOTIFICATION_PREFIX+".saved"; 129 133 public final static String NOTIFICATION_LOADED = 134 NOTIFICATION_PREFIX+".loaded"; 135 136 140 public final static String NOTIFICATION_MODIFIED = 141 NOTIFICATION_PREFIX+".modified"; 142 143 private static MBeanNotificationInfo[] NOTIFICATION_INFO = { 149 new MBeanNotificationInfo( 150 new String [] {NOTIFICATION_SAVED}, 151 Notification.class.getName(), 152 "Emitted when the configuration is saved"), 153 new MBeanNotificationInfo( 154 new String [] {NOTIFICATION_LOADED}, 155 Notification.class.getName(), 156 "Emitted when the configuration is loaded"), 157 new MBeanNotificationInfo( 158 new String [] {NOTIFICATION_MODIFIED}, 159 Notification.class.getName(), 160 "Emitted when the configuration is modified"), 161 }; 162 163 private volatile ScanManagerConfig config; 165 166 private String filename = null; 168 169 private volatile String configname = null; 172 173 private volatile SaveState status = CREATED; 176 177 202 public ScanDirConfig(String filename) { 203 this(filename,null); 204 } 205 206 211 public ScanDirConfig(String filename, ScanManagerConfig initialConfig) { 212 super(NOTIFICATION_INFO); 213 this.filename = filename; 214 this.config = initialConfig; 215 } 216 217 218 public void load() throws IOException { 220 if (filename == null) 221 throw new UnsupportedOperationException ("load"); 222 223 synchronized(this) { 224 config = new XmlConfigUtils(filename).readFromFile(); 225 if (configname != null) config = config.copy(configname); 226 else configname = config.getName(); 227 228 status=LOADED; 229 } 230 sendNotification(NOTIFICATION_LOADED); 231 } 232 233 public void save() throws IOException { 235 if (filename == null) 236 throw new UnsupportedOperationException ("load"); 237 synchronized (this) { 238 new XmlConfigUtils(filename).writeToFile(config); 239 status = SAVED; 240 } 241 sendNotification(NOTIFICATION_SAVED); 242 } 243 244 public ScanManagerConfig getConfiguration() { 246 synchronized (this) { 247 return XmlConfigUtils.xmlClone(config); 248 } 249 } 250 251 252 private void sendNotification(String type) { 254 final Object source = (objectName==null)?this:objectName; 255 final Notification n = new Notification(type,source, 256 getNextSeqNumber(), 257 "The configuration is "+ 258 type.substring(type.lastIndexOf('.')+1)); 259 sendNotification(n); 260 } 261 262 263 288 public ObjectName preRegister(MBeanServer server, ObjectName name) 289 throws Exception { 290 if (name == null) { 291 if (config == null) return null; 292 if (config.getName() == null) return null; 293 name = ScanManager. 294 makeMBeanName(ScanDirConfigMXBean.class,config.getName()); 295 } 296 objectName = name; 297 mbeanServer = server; 298 synchronized (this) { 299 configname = name.getKeyProperty("name"); 300 if (config == null) config = new ScanManagerConfig(configname); 301 else config = config.copy(configname); 302 } 303 return name; 304 } 305 306 315 public void postRegister(Boolean registrationDone) { 316 } 318 319 326 public void preDeregister() throws Exception { 327 } 329 330 335 public void postDeregister() { 336 } 338 339 public String getConfigFilename() { 341 return filename; 342 } 343 344 public void setConfiguration(ScanManagerConfig config) { 346 synchronized (this) { 347 if (config == null) { 348 this.config = null; 349 return; 350 } 351 352 if (configname == null) 353 configname = config.getName(); 354 355 this.config = config.copy(configname); 356 status = MODIFIED; 357 } 358 sendNotification(NOTIFICATION_MODIFIED); 359 } 360 361 public DirectoryScannerConfig 363 addDirectoryScanner(String name, String dir, String filePattern, 364 long sizeExceedsMaxBytes, long sinceLastModified) { 365 final DirectoryScannerConfig scanner = 366 new DirectoryScannerConfig(name); 367 scanner.setRootDirectory(dir); 368 if (filePattern!=null||sizeExceedsMaxBytes>0||sinceLastModified>0) { 369 final FileMatch filter = new FileMatch(); 370 filter.setFilePattern(filePattern); 371 filter.setSizeExceedsMaxBytes(sizeExceedsMaxBytes); 372 if (sinceLastModified > 0) 373 filter.setLastModifiedBefore(new Date (new Date ().getTime() 374 -sinceLastModified)); 375 scanner.addIncludeFiles(filter); 376 } 377 synchronized (this) { 378 config.putScan(scanner); 379 status = MODIFIED; 380 } 381 LOG.fine("config: "+config); 382 sendNotification(NOTIFICATION_MODIFIED); 383 return scanner; 384 } 385 386 public DirectoryScannerConfig removeDirectoryScanner(String name) 388 throws IOException , InstanceNotFoundException { 389 final DirectoryScannerConfig scanner; 390 synchronized (this) { 391 scanner = config.removeScan(name); 392 if (scanner == null) 393 throw new IllegalArgumentException (name+": scanner not found"); 394 status = MODIFIED; 395 } 396 sendNotification(NOTIFICATION_MODIFIED); 397 return scanner; 398 } 399 400 public SaveState getSaveState() { 402 return status; 403 } 404 405 static String DEFAULT = "DEFAULT"; 409 410 private static String getBasename(String name) { 411 final int dot = name.indexOf('.'); 412 if (dot<0) return name; 413 if (dot==0) return getBasename(name.substring(1)); 414 return name.substring(0,dot); 415 } 416 417 static String guessConfigName(String configFileName,String defaultFile) { 418 try { 419 if (configFileName == null) return DEFAULT; 420 final File f = new File (configFileName); 421 if (f.canRead()) { 422 final String confname = XmlConfigUtils.read(f).getName(); 423 if (confname != null && confname.length()>0) return confname; 424 } 425 final File f2 = new File (defaultFile); 426 if (f.equals(f2)) return DEFAULT; 427 final String guess = getBasename(f.getName()); 428 if (guess == null) return DEFAULT; 429 if (guess.length()==0) return DEFAULT; 430 return guess; 431 } catch (Exception x) { 432 return DEFAULT; 433 } 434 } 435 436 private volatile MBeanServer mbeanServer; 438 private volatile ObjectName objectName; 439 440 } 441 442 443 | Popular Tags |