diff options
| author | Martin Ritchie <ritchiem@apache.org> | 2009-07-22 17:11:51 +0000 |
|---|---|---|
| committer | Martin Ritchie <ritchiem@apache.org> | 2009-07-22 17:11:51 +0000 |
| commit | 9469490e6897bace7cc2c98afc5360b2a304a0bd (patch) | |
| tree | e5dea71b59e687d8ac3f3a02c7b3b505f4385167 | |
| parent | 02b35f8839baba896f9cb0f11471f214e559d10b (diff) | |
| download | qpid-python-9469490e6897bace7cc2c98afc5360b2a304a0bd.tar.gz | |
QPID-2001 : Add Default en_US messages logging along with a Velocity Template to generate static <Type>Messages classes that can be used for compile time vaildation of logging.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@796800 13f79535-47bb-0310-9956-ffa450edef68
5 files changed, 454 insertions, 35 deletions
diff --git a/java/broker/build.xml b/java/broker/build.xml index 1e8b817320..2a1bbb04e8 100644 --- a/java/broker/build.xml +++ b/java/broker/build.xml @@ -27,14 +27,61 @@ <property name="output.dir" value="${module.precompiled}/org/apache/qpid/server/filter/jms/selector"/> + <property name="gentools.home" location="${project.root}/../gentools" /> + <property name="generated.package" value="org/apache/qpid/server/logging/messages" /> + <property name="generated.dir" location="${module.precompiled}/${generated.package}" /> + <property name="velocity.compile.dir" value="${module.build}/velocity"/> + <property name="velocity.timestamp" location="${generated.dir}/velocity.timestamp" /> - <target name="precompile"> + + <target name="precompile" depends="gen_logging"> <mkdir dir="${output.dir}"/> + <!-- Compile Selcector Code --> <javacc target="src/main/grammar/SelectorParser.jj" outputdirectory="${output.dir}" javacchome="${project.root}/lib"/> </target> + <target name="compile_velocity" > + <mkdir dir="${velocity.compile.dir}" /> + <!-- Compile LogMessages Velocity Generator --> + <javac source="${java.source}" target="${java.target}" + destdir="${velocity.compile.dir}" debug="on" + deprecation="${javac.deprecation}" + srcdir="src/velocity/java" > + <classpath> + <pathelement path="${gentools.home}/lib/velocity-1.4.jar" /> + </classpath> + <compilerarg line="${javac.compiler.args}"/> + </javac> + </target> + + <property name="velocity.properties.dir" value="${project.root}/broker/src/main/java/org/apache/qpid/server/logging/messages"/> + + <target name="check_velocity_deps"> + <uptodate property="velocity.notRequired" targetfile="${velocity.timestamp}"> + <srcfiles dir="${velocity.properties.dir}" includes="LogMessages.properties" /> + <srcfiles dir="src/velocity/" includes="**/*.java **/*.vm" /> + </uptodate> + </target> + + <target name="gen_logging" depends="compile_velocity,check_velocity_deps" unless="velocity.notRequired"> + <mkdir dir="${generated.dir}"/> + <java classname="org.apache.qpid.server.logging.GenerateLogMessages" fork="true" dir="${gentools.home}/src" failonerror="true"> + <arg line="-j -o ${generated.dir} -t ${project.root}/broker/src/velocity/templates/org/apache/qpid/server/logging/messages"/> + <classpath> + <pathelement path="${project.root}/broker/src/main/java"/> + <pathelement path="${velocity.compile.dir}" /> + <fileset dir="${project.root}/lib"> + <include name="**/*.jar"/> + </fileset> + <pathelement path="${gentools.home}/lib/velocity-1.4.jar" /> + </classpath> + </java> + <touch file="${velocity.timestamp}" /> + </target> + + <target name="copy-etc-release" if="module.etc.exists" description="copy etc directory if it exists to build tree"> <copy todir="${module.release}/etc" failonerror="false" flatten="true"> <fileset dir="${module.etc}" excludes="*.conf,*.jpp"/> diff --git a/java/broker/src/main/java/org/apache/qpid/server/logging/BrokerMessages.java b/java/broker/src/main/java/org/apache/qpid/server/logging/BrokerMessages.java deleted file mode 100644 index e9cc7449cd..0000000000 --- a/java/broker/src/main/java/org/apache/qpid/server/logging/BrokerMessages.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - * - */ -package org.apache.qpid.server.logging; - -public class BrokerMessages -{ - - public static LogMessage BRK_1001(String version, String build) - { - return new LogMessage() - { - - }; - } - -} diff --git a/java/broker/src/main/java/org/apache/qpid/server/logging/messages/LogMessages_en_US.properties b/java/broker/src/main/java/org/apache/qpid/server/logging/messages/LogMessages_en_US.properties new file mode 100644 index 0000000000..868a611134 --- /dev/null +++ b/java/broker/src/main/java/org/apache/qpid/server/logging/messages/LogMessages_en_US.properties @@ -0,0 +1,82 @@ +#Broker +# 0 - Version +# 1 = Build +BRK-1001 = Startup : Version: {0} Build: {1} +# 0 - Transport +# 1 - Port +BRK-1002 = Starting : Listening on {0} port {1,number} +# 0 - Transport +# 1 - Port +BRK-1003 = Shuting down : {0} port {1,number} +BRK-1004 = Ready +BRK-1005 = Stopped +# 0 - path +BRK-1006 = Using configuration : {0} +# 0 - path +BRK-1007 = Using logging configuration : {0} + +#ManagementConsole +MNG-1001 = Startup +# 0 - Service +# 1 - Port +MNG-1002 = Starting : {0} : Listening on port {1,number} +# 0 - Service +# 1 - Port +MNG-1003 = Shuting down : {0} : port {1,number} +MNG-1004 = Ready +MNG-1005 = Stopped +# 0 - Path +MNG-1006 = Using SSL Keystore : {0} + +#VirtualHost +# 0 - name +VHT-1001 = Created : {0} +VHT-1002 = Closed + +#MessageStore +# 0 - name +MST-1001 = Created : {0} +# 0 - path +MST-1002 = Store location : {0} +MST-1003 = Closed +MST-1004 = Recovery Start +# 0 - queue name +MST-1005 = Recovery Start : {0} +# 0 - count +# 1 - queue count +MST-1006 = Recovered {0,number} messages for queue {0} +MST-1007 = Recovery Complete +# 0 - queue name +MST-1008 = Recovery Complete : {0} + +#Connection +# 0 - Client id +# 1 - Protocol Version +CON-1001 = Open : Client ID {0} : Protocol Version : {1} +CON-1002 = Close + +#Channel +# 0 - count +CHN-1001 = Create : Prefetch {0,number} +# 0 - flow +CHN-1002 = Flow {0} +CHN-1003 = Close + +#Queue +# 0 - owner +QUE-1001 = Create : Owner:{0} [AutoDelete] [Durable|Transient] [Priority:<levels>] +QUE-1002 = Deleted + +#Exchange +# 0 - type +# 1 - name +EXH-1001 = Create : [Durable] Type:{0} Name:{1} +EXH-1002 = Deleted + +#Binding +BND-1001 = Create [: Arguments : <key=value>] +BND-1002 = Deleted + +#Subscription +SUB-1001 = Create : [Durable] [Arguments : <key=value>] +SUB-1002 = Close diff --git a/java/broker/src/velocity/java/org/apache/qpid/server/logging/GenerateLogMessages.java b/java/broker/src/velocity/java/org/apache/qpid/server/logging/GenerateLogMessages.java new file mode 100644 index 0000000000..c62c51bb3f --- /dev/null +++ b/java/broker/src/velocity/java/org/apache/qpid/server/logging/GenerateLogMessages.java @@ -0,0 +1,247 @@ +package org.apache.qpid.server.logging;/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * + */ + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; + +import java.io.File; +import java.io.FileWriter; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.ResourceBundle; + +public class GenerateLogMessages +{ + private static String _tmplDir; + private String _outputDir; + + public static void main(String[] args) + { + GenerateLogMessages generator = null; + try + { + generator = new GenerateLogMessages(args); + } + catch (IllegalAccessException iae) + { + System.exit(-1); + } + catch (Exception e) + { + e.printStackTrace(); + System.exit(-1); + } + + try + { + generator.run(); + } + catch (InvalidTypeException e) + { + System.err.println(e.getMessage()); + System.exit(-1); + } + catch (Exception e) + { + e.printStackTrace(); + System.exit(-1); + } + } + + GenerateLogMessages(String[] args) throws Exception + { + processArgs(args); + + if (_tmplDir == null||_outputDir == null) + { + showUsage(); + throw new IllegalAccessException(); + } + + + /* first, we init the runtime engine. Defaults are fine. */ + Properties props = new Properties(); + props.setProperty("file.resource.loader.path", _tmplDir); + + Velocity.init(props); + } + + private void showUsage() + { + System.out.println("Broker LogMessageGenerator v.0.0"); + System.out.println("Usage: GenerateLogMessages: -t tmplDir"); + System.out.println(" where -t tmplDir: Find templates in tmplDir."); + System.out.println(" -o outDir: Use outDir as the output dir."); + } + + public void run() throws InvalidTypeException, Exception + { + /* lets make a Context and put data into it */ + createMessageClass("Broker", "BRK"); + createMessageClass("ManagementConsole", "MNG"); + createMessageClass("VirtualHost", "VHT"); + createMessageClass("MessageStore", "MST"); + createMessageClass("Connection", "CON"); + createMessageClass("Channel", "CHN"); + createMessageClass("Queue", "QUE"); + createMessageClass("Exchange", "EXH"); + createMessageClass("Binding", "BND"); + createMessageClass("Subscription", "SUB"); + } + + /** + * Process the args for a -t value for the template location + * + * @param args + */ + private void processArgs(String[] args) + { + // Crude but simple... + for (int i = 0; i < args.length; i++) + { + String arg = args[i]; + if (arg.charAt(0) == '-') + { + switch (arg.charAt(1)) + { + case 'o': + case 'O': + if (++i < args.length) + { + _outputDir = args[i]; + } + break; + case 't': + case 'T': + if (++i < args.length) + { + _tmplDir = args[i]; + } + break; + } + } + } + } + + private void createMessageClass(String className, String typeIdentifier) + throws InvalidTypeException, Exception + { + VelocityContext context = new VelocityContext(); + + HashMap<String, Object> typeData = prepareType(className, typeIdentifier); + + context.put("type", typeData); + + /* lets render a template */ + FileWriter output = new FileWriter(_outputDir + File.separator + className + "Messages.java"); + + Velocity.mergeTemplate("LogMessages.vm", context, output); + + output.flush(); + output.close(); + } + + private HashMap<String, Object> prepareType(String messsageName, String messageKey) throws InvalidTypeException + { + ResourceBundle _messages = ResourceBundle.getBundle("org.apache.qpid.server.logging.messages.LogMessages"); + + Enumeration<String> messageKeys = _messages.getKeys(); + + HashMap<String, Object> typeData = new HashMap<String, Object>(); + typeData.put("name", messsageName); + + List<HashMap> messageList = new LinkedList<HashMap>(); + typeData.put("list", messageList); + + while (messageKeys.hasMoreElements()) + { + HashMap<String, Object> item = new HashMap<String, Object>(); + + //Add MessageName to amp + String message = messageKeys.nextElement(); + + if (message.startsWith(messageKey)) + { + item.put("methodName", message.replace('-','_')); + item.put("name", message); + + item.put("format", _messages.getString(message)); + + String[] parametersString = _messages.getString(message).split("\\{"); + + // Add P + List<HashMap<String, String>> parameters = new LinkedList<HashMap<String, String>>(); + // Skip 0 as that will not be the first entry + // Text {n[,type]} + if (parametersString.length > 1) + { + for (int index = 1; index < parametersString.length; index++) + { + HashMap<String, String> parameter = new HashMap<String, String>(); + + int typeIndex = parametersString[index].indexOf(","); + + String type; + if (typeIndex == -1) + { + type = "String"; + } + else + { + int typeIndexEnd = parametersString[index].indexOf("}", typeIndex); + String typeString = parametersString[index].substring(typeIndex + 1, typeIndexEnd); + if (typeString.equalsIgnoreCase("number")) + { + type = "Integer"; + } + else + { + throw new InvalidTypeException("Invalid type(" + typeString + ") index (" + parameter.size() + ") in message:" + _messages.getString(message)); + } + + } + + parameter.put("type", type); + parameter.put("name", "param" + index); + + parameters.add(parameter); + } + } + + item.put("parameters", parameters); + messageList.add(item); + } + } + + return typeData; + } + + private class InvalidTypeException extends Throwable + { + public InvalidTypeException(String message) + { + super(message); + } + } +}
\ No newline at end of file diff --git a/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm b/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm new file mode 100644 index 0000000000..7360b8c403 --- /dev/null +++ b/java/broker/src/velocity/templates/org/apache/qpid/server/logging/messages/LogMessages.vm @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * + */ +package org.apache.qpid.server.logging.messages; + +import org.apache.qpid.server.logging.LogMessage; +import org.apache.qpid.server.registry.ApplicationRegistry; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; + +/** + * Generated Using GeneratedLogMessages and LogMessages.vm + * + * This file is based on the content of LogMessages.properties + * + * It is generated so that we can provide compile time validation of the + * message parameters + */ +public class ${type.name}Messages +{ + static ResourceBundle _messages; + static MessageFormat _formatter; + + static + { + Locale currentLocale = ApplicationRegistry.getInstance().getConfiguration().getLocale(); + + _messages = ResourceBundle.getBundle("org.apache.qpid.server.logging.messages.LogMessages", + currentLocale); + + _formatter = new MessageFormat(""); + _formatter.setLocale(currentLocale); + } + +#foreach( $message in ${type.list} ) + /** + * Log a ${type.name} message of the Format: + * <pre>${message.format}</pre> + */ + public static LogMessage ${message.methodName}(#foreach($parameter in ${message.parameters})${parameter.type} ${parameter.name}#if (${velocityCount} != ${message.parameters.size()} ),#end +#end) + { + final Object[] messageArguments = {#foreach($parameter in ${message.parameters})${parameter.name}#if (${velocityCount} != ${message.parameters.size()} ),#end#end}; + + _formatter.applyPattern(_messages.getString("${message.name}")); + return new LogMessage() + { + public String toString() + { + return "${message.name} : " + _formatter.format(messageArguments); + } + }; + + } + +#end + +} |
