Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Log4J configurabile da Extension Point"

Da WikiSitech.
Vai alla navigazioneVai alla ricerca
Riga 30: Riga 30:
  
 
==Definizione degli Extension Point==
 
==Definizione degli Extension Point==
Ora bisogna definire gli Extension Point di configurazione
 
  
 
<code xml>
 
<code xml>
Riga 237: Riga 236:
 
<?xml version='1.0' encoding='UTF-8'?>
 
<?xml version='1.0' encoding='UTF-8'?>
 
<!-- Schema file written by PDE -->
 
<!-- Schema file written by PDE -->
<schema targetNamespace="org.rimondini.rcp.framework.logger">
+
<schema targetNamespace="org.logger.log4j">
 
<annotation>
 
<annotation>
 
       <appInfo>
 
       <appInfo>
         <meta.schema plugin="org.rimondini.rcp.framework.logger" id="LoggerDefinition" name="LoggerDefinition"/>
+
         <meta.schema plugin="org.logger.log4j" id="LoggerDefinition" name="LoggerDefinition"/>
 
       </appInfo>
 
       </appInfo>
 
       <documentation>
 
       <documentation>

Versione delle 17:20, 16 nov 2007

Plugin Log4J configurabile da Extension Point

Per avere un logger Log4J configurabile da extension point bisogna prima di tutto crearsi il progetto di tipo plugin:

LoggerNewProject.jpg

LoggerNewProject2.jpg

Dopo bisogna convertirlo in un progetto Java Entermitted Template (JET):

LoggerJetConversion.jpg

LoggerJetConversion2.jpg

e configurarlo in maniera corretta:

LoggerJetConversion3.jpg

A questo punto è possibile iniziare la scrittura del extension point di configurazione....

Definizione degli Extension Point

<?xml version='1.0' encoding='UTF-8'?> <schema targetNamespace="org.logger.log4j"> <annotation>

     <appInfo>
        <meta.schema plugin="org.logger.log4j" id="AppenderDefinition" name="AppenderDefinition"/>
     </appInfo>
     <documentation>
        [Enter description of this extension point.]
     </documentation>
  </annotation>
  <element name="extension">
     <complexType>
        <sequence>
           <element ref="DailyAppender" minOccurs="1" maxOccurs="unbounded"/>
           <element ref="RollingAppender" minOccurs="0" maxOccurs="unbounded"/>
        </sequence>
        <attribute name="point" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="id" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="name" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
              <appInfo>
                 <meta.attribute translatable="true"/>
              </appInfo>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <element name="DailyAppender">
     <complexType>
        <attribute name="name" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="file" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="append" type="boolean" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="layout_class" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
              <appInfo>
                 <meta.attribute kind="java"/>
              </appInfo>
           </annotation>
        </attribute>
        <attribute name="layout_conversion_pattern" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="date_pattern" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <element name="RollingAppender">
     <complexType>
        <attribute name="name" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="file" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="append" type="boolean" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="layout_class" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
              <appInfo>
                 <meta.attribute kind="java"/>
              </appInfo>
           </annotation>
        </attribute>
        <attribute name="layout_conversion_pattern" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="max_size" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="max_backup" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <annotation>
     <appInfo>
        <meta.section type="since"/>
     </appInfo>
     <documentation>
        [Enter the first release in which this extension point appears.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="examples"/>
     </appInfo>
     <documentation>
        [Enter extension point usage example here.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="apiInfo"/>
     </appInfo>
     <documentation>
        [Enter API information here.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="implementation"/>
     </appInfo>
     <documentation>
        [Enter information about supplied implementation of this extension point.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="copyright"/>
     </appInfo>
     <documentation>
        
     </documentation>
  </annotation>

</schema>

org.logger.log4j.LoggerDefinition.exsd <?xml version='1.0' encoding='UTF-8'?> <schema targetNamespace="org.logger.log4j"> <annotation>

     <appInfo>
        <meta.schema plugin="org.logger.log4j" id="LoggerDefinition" name="LoggerDefinition"/>
     </appInfo>
     <documentation>
        [Enter description of this extension point.]
     </documentation>
  </annotation>
  <element name="extension">
     <complexType>
        <sequence>
           <element ref="Logger" minOccurs="1" maxOccurs="unbounded"/>
        </sequence>
        <attribute name="point" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="id" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="name" type="string">
           <annotation>
              <documentation>
                 
              </documentation>
              <appInfo>
                 <meta.attribute translatable="true"/>
              </appInfo>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <element name="Logger">
     <complexType>
        <sequence minOccurs="1" maxOccurs="unbounded">
           <element ref="Appender"/>
        </sequence>
        <attribute name="logger" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
        <attribute name="level" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
           <simpleType>
              <restriction base="string">
                 <enumeration value="error">
                 </enumeration>
                 <enumeration value="warn">
                 </enumeration>
                 <enumeration value="info">
                 </enumeration>
                 <enumeration value="debug">
                 </enumeration>
                 <enumeration value="trace">
                 </enumeration>
              </restriction>
           </simpleType>
        </attribute>
        <attribute name="log_in_console" type="boolean" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <element name="Appender">
     <complexType>
        <attribute name="name" type="string" use="required">
           <annotation>
              <documentation>
                 
              </documentation>
           </annotation>
        </attribute>
     </complexType>
  </element>
  <annotation>
     <appInfo>
        <meta.section type="since"/>
     </appInfo>
     <documentation>
        [Enter the first release in which this extension point appears.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="examples"/>
     </appInfo>
     <documentation>
        [Enter extension point usage example here.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="apiInfo"/>
     </appInfo>
     <documentation>
        [Enter API information here.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="implementation"/>
     </appInfo>
     <documentation>
        [Enter information about supplied implementation of this extension point.]
     </documentation>
  </annotation>
  <annotation>
     <appInfo>
        <meta.section type="copyright"/>
     </appInfo>
     <documentation>
        
     </documentation>
  </annotation>

</schema>

Definizione dei Bean

public class LoggerDefinition implements IDefinitionBean {

  public String logger;
  public LOG_LEVEL level = LOG_LEVEL.debug;
  public List<String> appenders = new ArrayList<String>();
  public boolean log_in_console = true;
  public boolean runtime = false;
  public String getAppenders() {
     StringBuffer sb = new StringBuffer();
     for (Iterator<String> it = appenders.iterator(); it.hasNext();) {
        sb.append(it.next());
        if (it.hasNext()) {
           sb.append(","); //$NON-NLS-1$
        }
     }
     return sb.toString();
  }

}

public abstract class AppenderDefinition {

  public String appender, file;
  public String layout_class = "org.apache.log4j.PatternLayout"; //$NON-NLS-1$
  public String layout_conversion_pattern = "[%d] [%-5p] [%c{3}] - %m %n"; //$NON-NLS-1$
  public boolean append;

}

public class DailyAppenderDef extends AppenderDefinition {

  public String date_pattern = "'.'yyyy-MM-dd"; //$NON-NLS-1$

}

public class RollingAppenderDef extends AppenderDefinition {

  public String max_size = "500KB"; //$NON-NLS-1$
  public String max_backup = "1"; //$NON-NLS-1$

}

public class Log4JDefinitionBean implements IDefinitionBean {

  private static Log4JDefinitionBean instance;
  private static boolean toinit = true;
  public static void initialize() {
     if (toinit) {
        instance = new Log4JDefinitionBean();
        AppenderDefinitionEP.readConfig();
        LoggerDefinitionEP.readConfig();
        Log4JGenerator g = new Log4JGenerator();
        ByteArrayInputStream bis = new ByteArrayInputStream(g.generate(instance).getBytes());
        LoggerFactory.initialize(bis);
        toinit = false;
     }
  }
  static {
     initialize();
  }
  public static Log4JDefinitionBean getInstance() {
     return instance;
  }
  public LOG_LEVEL rootLevel = LOG_LEVEL.debug;
  public List<LoggerDefinition> loggers = new ArrayList<LoggerDefinition>();
  public List<AppenderDefinition> appenders = new ArrayList<AppenderDefinition>();

}

Lettura degli Extension Point

public class AppenderDefinitionEP {

  private static final String EXTPOINT_ID = "org.logger.log4j.AppenderDefinition"; //$NON-NLS-1$
  @SuppressWarnings("unchecked")//$NON-NLS-1$
  public static void readConfig() {
     IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(EXTPOINT_ID);
     if (ep != null) {
        IExtension[] extensions = ep.getExtensions();
        IExtension ex;
        IConfigurationElement[] configurations;
        IConfigurationElement conf;
        if (extensions != null && extensions.length > 0) {
           for (int e = 0; e < extensions.length;) {
              ex = extensions[e];
              configurations = ex.getConfigurationElements();
              if (configurations != null && configurations.length > 0) {
                 for (int c = 0; c < configurations.length;) {
                    conf = configurations[c];
                    if ("DailyAppender".equals(conf.getName())) { //$NON-NLS-1$
                       DailyAppenderDef appender = new DailyAppenderDef();
                       appender.appender = conf.getAttribute("name"); //$NON-NLS-1$
                       appender.file = conf.getAttribute("file"); //$NON-NLS-1$
                       appender.append = Boolean.valueOf(conf.getAttribute("append")); //$NON-NLS-1$
                       if (conf.getAttribute("date_pattern") != null) { //$NON-NLS-1$
                          appender.date_pattern = conf.getAttribute("date_pattern"); //$NON-NLS-1$
                       }
                       if (conf.getAttribute("layout_class") != null) { //$NON-NLS-1$
                          appender.layout_class = conf.getAttribute("layout_class"); //$NON-NLS-1$
                       }
                       if (conf.getAttribute("layout_conversion_pattern") != null) { //$NON-NLS-1$
                          appender.layout_conversion_pattern = conf.getAttribute("layout_conversion_pattern"); //$NON-NLS-1$
                        }
                       Log4JDefinitionBean.getInstance().appenders.add(appender);
                    } else if ("RollingAppender".equals(conf.getName())) { //$NON-NLS-1$
                       RollingAppenderDef appender = new RollingAppenderDef();
                       appender.appender = conf.getAttribute("name"); //$NON-NLS-1$
                       appender.file = conf.getAttribute("file"); //$NON-NLS-1$
                       appender.append = Boolean.valueOf(conf.getAttribute("append")); //$NON-NLS-1$
                       if (conf.getAttribute("max_backup") != null) { //$NON-NLS-1$
                          appender.max_backup = conf.getAttribute("max_backup"); //$NON-NLS-1$
                       }
                       if (conf.getAttribute("max_size") != null) { //$NON-NLS-1$
                          appender.max_size = conf.getAttribute("max_size"); //$NON-NLS-1$
                       }
                       if (conf.getAttribute("layout_class") != null) { //$NON-NLS-1$
                          appender.layout_class = conf.getAttribute("layout_class"); //$NON-NLS-1$
                       }
                       if (conf.getAttribute("layout_conversion_pattern") != null) { //$NON-NLS-1$

appender.layout_conversion_pattern = conf.getAttribute("layout_conversion_pattern"); //$NON-NLS-1$

                       }
                       Log4JDefinitionBean.getInstance().appenders.add(appender);
                    }
                    c++;
                 }
              }
              e++;
           }
        }
     }
  }

}

JET Template

Questo è un template JET che permette la generzione di un file di configurazione di log4j <%@ jet package="org.logger.log4j.logger.generator" imports="org.logger.log4j.logger.bean.*" class="Log4JGenerator" %><% Log4JDefinitionBean bean = (Log4JDefinitionBean)argument; %> log4j.rootLogger=<%=bean.rootLevel.toString()%>

  1. -----> Logger definition

<% for(java.util.Iterator<LoggerDefinition> it = bean.loggers.iterator(); it.hasNext();) { LoggerDefinition o = it.next(); if(o.appenders.size() > 0) { %> log4j.logger.<%=o.logger%>=<%=o.level.toString()%>,<%= o.log_in_console ? " APPENDER_CONSOLE, " : " "%><%=o.getAppenders()%> <%} } %>

  1. ----------------------------------------------------------------
  2. APPENDER_CONSOLE appender
  3. ----------------------------------------------------------------

log4j.appender.APPENDER_CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.APPENDER_CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.APPENDER_CONSOLE.layout.ConversionPattern=[%d] [%-5p] [%c{3}] - %m %n <% for(java.util.Iterator<AppenderDefinition> it = bean.appenders.iterator(); it.hasNext();) { AppenderDefinition o = it.next(); %>

  1. ----------------------------------------------------------------
  2. <%=o.appender %> appender
  3. ----------------------------------------------------------------

log4j.appender.<%=o.appender %>.File=<%=o.file%> log4j.appender.<%=o.appender %>.layout=<%=o.layout_class%> log4j.appender.<%=o.appender %>.layout.ConversionPattern=<%=o.layout_conversion_pattern%> log4j.appender.<%=o.appender %>.append=<%=o.append%> <%if(o instanceof DailyAppenderDefinition) { %>

  1. Daily LOG

log4j.appender.<%=o.appender %>=org.apache.log4j.DailyRollingFileAppender log4j.appender.<%=o.appender %>.DatePattern=<%=((DailyAppenderDefinition)o).date_pattern%> <% } else if(o instanceof RollingAppenderDefinition) {%>

  1. Rolling LOG

log4j.appender.<%=o.appender %>=org.apache.log4j.RollingFileAppender log4j.appender.<%=o.appender %>.MaxFileSize=<%=((RollingAppenderDefinition)o).max_size%> log4j.appender.<%=o.appender %>.MaxBackupIndex=<%=((RollingAppenderDefinition)o).max_backup%>

<% } }%>

quando il file viene salvato JET genera automaticamente la classe che produce quest'output.