1 /* Copyright (C) 2004 Vladimir Roubtsov. All rights reserved. 2 * 3 * This program and the accompanying materials are made available under 4 * the terms of the Common Public License v1.0 which accompanies this distribution, 5 * and is available at http://www.eclipse.org/legal/cpl-v10.html 6 * 7 * $Id: Processor.java,v 1.1.2.1 2004/07/16 23:32:03 vlad_r Exp $ 8 */ 9 package com.vladium.emma; 10 11 import java.util.Properties; 12 13 import com.vladium.logging.Logger; 14 import com.vladium.util.IProperties; 15 import com.vladium.util.asserts.$assert; 16 17 // ---------------------------------------------------------------------------- 18 /** 19 * @author Vlad Roubtsov, (C) 2004 20 */ 21 public 22 abstract class Processor 23 { 24 // public: ................................................................ 25 26 run()27 public synchronized void run () 28 { 29 validateState (); 30 31 // load tool properties: 32 final IProperties toolProperties; 33 { 34 final IProperties appProperties = EMMAProperties.getAppProperties (); 35 36 toolProperties = IProperties.Factory.combine (m_propertyOverrides, appProperties); 37 } 38 if ($assert.ENABLED) $assert.ASSERT (toolProperties != null, "toolProperties is null"); // can be empty, though 39 40 final Logger current = Logger.getLogger (); 41 final Logger log = AppLoggers.create (m_appName, toolProperties, current); 42 43 if (log.atTRACE1 ()) 44 { 45 log.trace1 ("run", "complete tool properties:"); 46 toolProperties.list (log.getWriter ()); 47 } 48 49 try 50 { 51 Logger.push (log); 52 m_log = log; 53 54 _run (toolProperties); 55 } 56 finally 57 { 58 if (m_log != null) 59 { 60 Logger.pop (m_log); 61 m_log = null; 62 } 63 } 64 } 65 66 setAppName(final String appName)67 public synchronized final void setAppName (final String appName) 68 { 69 m_appName = appName; 70 } 71 72 /** 73 * 74 * @param overrides [may be null (unsets the previous overrides)] 75 */ setPropertyOverrides(final Properties overrides)76 public synchronized final void setPropertyOverrides (final Properties overrides) 77 { 78 m_propertyOverrides = EMMAProperties.wrap (overrides); 79 } 80 81 /** 82 * 83 * @param overrides [may be null (unsets the previous overrides)] 84 */ setPropertyOverrides(final IProperties overrides)85 public synchronized final void setPropertyOverrides (final IProperties overrides) 86 { 87 m_propertyOverrides = overrides; 88 } 89 90 // protected: ............................................................. 91 92 Processor()93 protected Processor () 94 { 95 // not publicly instantiable 96 } 97 _run(IProperties toolProperties)98 protected abstract void _run (IProperties toolProperties); 99 100 validateState()101 protected void validateState () 102 { 103 // no Processor state needs validation 104 105 // [m_appName allowed to be null] 106 // [m_propertyOverrides allowed to be null] 107 } 108 109 110 protected String m_appName; // used as logging prefix, can be null 111 protected IProperties m_propertyOverrides; // user override; can be null/empty for run() 112 protected Logger m_log; // not null only within run() 113 114 // package: ............................................................... 115 116 // private: ............................................................... 117 118 } // end of class 119 // ----------------------------------------------------------------------------