| 1 | /* |
| 2 | * Copyright 2005-2006 The RbUtils Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | // $Id: LoggerCreator.java,v 1.1 2006/11/07 03:11:38 moishi Exp $ |
| 19 | |
| 20 | package org.ktc.rbutils.rb; |
| 21 | |
| 22 | import java.io.BufferedOutputStream; |
| 23 | import java.io.File; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.OutputStream; |
| 27 | |
| 28 | import org.apache.commons.lang.Validate; |
| 29 | import org.ktc.rbutils.api.RbUtilsRuntimeException; |
| 30 | import org.ktc.rbutils.api.UnSpecifiedFileException; |
| 31 | import org.ktc.rbutils.api.UnknownTaskException; |
| 32 | import org.ktc.rbutils.api.audit.Logger; |
| 33 | import org.ktc.rbutils.api.file.FileTools; |
| 34 | import org.ktc.rbutils.rb.check.DefaultLogger; |
| 35 | import org.ktc.rbutils.rb.check.XmlLogger; |
| 36 | |
| 37 | /** |
| 38 | * Utility class that helps for logger creation. |
| 39 | * <p> |
| 40 | * All RbUtils loggers can be created with this class according to a taskname. |
| 41 | * @since RbUtils 0.9.3.3 |
| 42 | * @version $Revision: 1.1 $ |
| 43 | * @author moishi |
| 44 | */ |
| 45 | public class LoggerCreator { |
| 46 | /* |
| 47 | * TODO duplication - this must be refactor with AbstractRbTask |
| 48 | */ |
| 49 | |
| 50 | /** |
| 51 | * Instanciates a new Logger. |
| 52 | */ |
| 53 | public LoggerCreator() { |
| 54 | super(); |
| 55 | } |
| 56 | |
| 57 | /** The logger type. */ |
| 58 | private LoggerType loggerType = LoggerType.PLAIN; |
| 59 | /** The file to output to. */ |
| 60 | private File outputFile; |
| 61 | |
| 62 | /** |
| 63 | * Returns the type of this logger. |
| 64 | * @return the type of the ant logger. |
| 65 | */ |
| 66 | public LoggerType getLoggerType() { |
| 67 | return this.loggerType; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the type of the logger. |
| 72 | * @param loggertype the type of the logger. |
| 73 | */ |
| 74 | public void setLoggerType(final LoggerType loggertype) { |
| 75 | loggerType = loggertype; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets the type of logger. |
| 80 | * @param loggertype the type of logger to be set. |
| 81 | * @throws RbUtilsRuntimeException if the provided String does not represent a logger type. |
| 82 | */ |
| 83 | public void setLoggerType(final String loggertype) { |
| 84 | // Validate argument |
| 85 | if (!LoggerType.isLoggerTypeValue(loggertype)) { |
| 86 | throw new RbUtilsRuntimeException("Invalid logger type: " + loggertype); |
| 87 | } |
| 88 | setLoggerType(LoggerType.getEnum(loggertype)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the output file of this ant logger. |
| 93 | * @return the output file of this ant logger. |
| 94 | */ |
| 95 | public File getOutputFile() { |
| 96 | return this.outputFile; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set the file to output to. |
| 101 | * @param theOutputFile the file to output to. |
| 102 | */ |
| 103 | public void setOutputFile(final File theOutputFile) { |
| 104 | outputFile = theOutputFile; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set the file to output to. If <code>path2File</code> is <code>null</code>, the file is |
| 109 | * not set. |
| 110 | * @param path2File the full path to the file. |
| 111 | */ |
| 112 | public void setOutputFile(final String path2File) { |
| 113 | if (path2File != null) { |
| 114 | outputFile = new File(path2File); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a logger according to a task. TODO Javadoc - to be improved. |
| 120 | * @param taskname the task to be associated to the logger. |
| 121 | * @return a logger according to the task. |
| 122 | * @throws IOException if the logger relies on a file and this file cannot be created. |
| 123 | * @throws UnknownTaskException if the task is not supported. |
| 124 | * @throws UnSpecifiedFileException if the outputFile is not set and the the task relies on xml |
| 125 | * logging. |
| 126 | */ |
| 127 | public Logger createLogger(final String taskname) throws IOException, UnSpecifiedFileException { |
| 128 | final Logger logger; |
| 129 | // if ((loggerType != null) && LOGGERTYPE_XML.equals(loggerType.getValue())) { |
| 130 | if (LoggerType.XML.equals(loggerType)) { |
| 131 | logger = createXmlLogger(taskname); |
| 132 | } |
| 133 | else { |
| 134 | logger = createDefaultLogger(taskname); |
| 135 | } |
| 136 | // TODO quiet logger |
| 137 | return logger; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Creates a default logger according to a task. |
| 142 | * @return a DefaultLogger instance. |
| 143 | * @param taskname the task associated to the logger. |
| 144 | * @throws IOException if the logger relies on a file and this file cannot be created. |
| 145 | * @throws UnknownTaskException if the task is not supported. |
| 146 | */ |
| 147 | private Logger createDefaultLogger(final String taskname) throws IOException { |
| 148 | // Sets the used output stream by this logger |
| 149 | final OutputStream msgStream; |
| 150 | final OutputStream errorStream; |
| 151 | if (outputFile == null) { |
| 152 | // msgStream = new LogOutputStream(task, Project.MSG_DEBUG); |
| 153 | // errorStream = new LogOutputStream(task, Project.MSG_ERR); |
| 154 | msgStream = System.out; |
| 155 | errorStream = System.err; |
| 156 | } |
| 157 | else { |
| 158 | FileTools.forceNewFile(outputFile); |
| 159 | msgStream = new BufferedOutputStream(new FileOutputStream(outputFile)); |
| 160 | errorStream = msgStream; |
| 161 | } |
| 162 | |
| 163 | // Instanciates the default logger |
| 164 | Logger logger = null; |
| 165 | // if (taskname instanceof RbCheckerTask) { |
| 166 | if (CommonProps.RBCHECKER_TASKNAME.equals(taskname)) { |
| 167 | logger = new DefaultLogger(msgStream, true, errorStream, true); |
| 168 | } |
| 169 | // else if (taskname instanceof RbGeneratorTask) { |
| 170 | else if (CommonProps.RBGENERATOR_TASKNAME.equals(taskname)) { |
| 171 | logger = new org.ktc.rbutils.rb.generation.DefaultLogger(msgStream, true, errorStream, |
| 172 | true); |
| 173 | } |
| 174 | else { |
| 175 | // throw new BuildException(UNSUPPORTEDTASK_MSG + taskname); |
| 176 | throw new UnknownTaskException(taskname); |
| 177 | } |
| 178 | return logger; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Creates a xml logger according to the task. |
| 183 | * @return an XmlLogger instance. |
| 184 | * @param taskname the task associated to the logger. |
| 185 | * @throws IOException if the logging file cannot be created. |
| 186 | * @throws UnSpecifiedFileException if the outputFile is not set. |
| 187 | * @throws UnknownTaskException if the task is not supported. |
| 188 | */ |
| 189 | private Logger createXmlLogger(final String taskname) throws IOException, |
| 190 | UnSpecifiedFileException |
| 191 | { |
| 192 | Validate.notNull(taskname); |
| 193 | |
| 194 | final Logger logger; |
| 195 | if (outputFile == null) { |
| 196 | throw new UnSpecifiedFileException("outputFile must not be null"); |
| 197 | } |
| 198 | |
| 199 | if (CommonProps.RBCHECKER_TASKNAME.equals(taskname)) { |
| 200 | // if (taskname instanceof RbCheckerTask) { |
| 201 | logger = new XmlLogger(outputFile); |
| 202 | } |
| 203 | else if (CommonProps.RBGENERATOR_TASKNAME.equals(taskname)) { |
| 204 | // else if (taskname instanceof RbGeneratorTask) { |
| 205 | logger = new org.ktc.rbutils.rb.generation.XmlLogger(outputFile); |
| 206 | } |
| 207 | else { |
| 208 | // throw new RbUtilsException(UNSUPPORTEDTASK_MSG + taskname.getTaskName()); |
| 209 | // throw new RbUtilsException(CommonProps.RBUTILS_UNKNONWN_TASKNAME + taskname); |
| 210 | throw new UnknownTaskException(taskname); |
| 211 | } |
| 212 | return logger; |
| 213 | } |
| 214 | |
| 215 | } |