001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.trigger;
019:
020: import java.io.BufferedWriter;
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.OutputStreamWriter;
026: import java.io.Writer;
027:
028: import org.apache.ivy.core.IvyPatternHelper;
029: import org.apache.ivy.core.event.IvyEvent;
030: import org.apache.ivy.core.resolve.ResolveProcessException;
031: import org.apache.ivy.util.Message;
032:
033: /**
034: * A trigger performing logging.
035: * <p>
036: * The implementation is widely inspired by Ant Echo task.
037: * </p>
038: */
039: public class LogTrigger extends AbstractTrigger {
040: private static final String LINE_SEPARATOR = System
041: .getProperty("line.separator");
042:
043: private String message = "";
044:
045: private File file = null;
046:
047: private boolean append = true;
048: /** encoding; set to null or empty means 'default' */
049: private String encoding = "";
050:
051: public void progress(IvyEvent event) {
052: log(IvyPatternHelper.substituteVariables(message, event
053: .getAttributes()));
054: }
055:
056: /**
057: * Logs the given message.
058: *
059: * @param message the message to log
060: */
061: protected void log(String message) {
062: if (file == null) {
063: Message.info(message);
064: } else {
065: Writer out = null;
066: try {
067: // we add a line separator here for consistency with Message.info which creates a
068: // new line each time.
069: // we use the system dependent line separator to ease reading the log file
070: message += LINE_SEPARATOR;
071: String filename = file.getAbsolutePath();
072: if (encoding == null || encoding.length() == 0) {
073: out = new FileWriter(filename, append);
074: } else {
075: out = new BufferedWriter(new OutputStreamWriter(
076: new FileOutputStream(filename, append),
077: encoding));
078: }
079: out.write(message, 0, message.length());
080: } catch (IOException e) {
081: throw new ResolveProcessException(e);
082: } finally {
083: if (out != null) {
084: try {
085: out.close();
086: } catch (IOException e) {
087: throw new ResolveProcessException(e);
088: }
089: }
090: }
091: }
092: }
093:
094: /**
095: * Message to write.
096: *
097: * @param msg Sets the value for the message variable.
098: */
099: public void setMessage(String msg) {
100: this .message = msg;
101: }
102:
103: /**
104: * File to write to.
105: * @param file the file to write to, if not set, echo to
106: * standard Ivy logging
107: */
108: public void setFile(String file) {
109: this .file = new File(file);
110: }
111:
112: /**
113: * If true, append to existing file.
114: * @param append if true, append to existing file, default
115: * is false.
116: */
117: public void setAppend(boolean append) {
118: this .append = append;
119: }
120:
121: /**
122: * Declare the encoding to use when outputting to a file;
123: * Use "" for the platform's default encoding.
124: * @param encoding the character encoding to use.
125: */
126: public void setEncoding(String encoding) {
127: this.encoding = encoding;
128: }
129: }
|