001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.sysout;
021
022import java.io.File;
023import java.io.FileReader;
024import java.io.FilenameFilter;
025import java.io.LineNumberReader;
026import java.io.PrintStream;
027import java.util.Enumeration;
028import java.util.Locale;
029import java.util.Properties;
030import java.util.TimeZone;
031
032import org.apache.isis.core.commons.exceptions.IsisException;
033import org.apache.isis.core.commons.lang.CloseableExtensions;
034import org.apache.isis.core.runtime.about.AboutIsis;
035
036public class SystemPrinter {
037
038    private final PrintStream output;
039
040    public SystemPrinter() {
041        this(System.out);
042    }
043
044    public SystemPrinter(final PrintStream output) {
045        this.output = output;
046    }
047
048    protected PrintStream getOutput() {
049        return output;
050    }
051
052    void print(final String string) {
053        output.println(string);
054    }
055
056    void printBlock(final String title) {
057        print("");
058        print("------------------------------------------");
059        print(title);
060        print("------------------------------------------");
061    }
062
063    public void printDiagnostics() {
064        print("------- Apache Isis diagnostics report -------");
065        printVersion();
066
067        printBlock("System properties");
068        final Properties properties = System.getProperties();
069        final Enumeration<?> propertyNames = properties.propertyNames();
070        while (propertyNames.hasMoreElements()) {
071            final String name = (String) propertyNames.nextElement();
072            final String property = properties.getProperty(name);
073            final StringBuilder buf = new StringBuilder();
074            if (name.endsWith(".path") || name.endsWith(".dirs")) {
075                final String[] split = property.split(":");
076                buf.append(split[0]);
077                for (int i = 1; i < split.length; i++) {
078                    buf.append("\n\t\t" + split[i]);
079                }
080            }
081            print(name + "= " + buf.toString());
082        }
083
084        File file = new File("../lib");
085        if (file.isDirectory()) {
086            final String[] files = file.list(new FilenameFilter() {
087                @Override
088                public boolean accept(final File dir, final String name) {
089                    return name.endsWith(".jar");
090                }
091            });
092            printBlock("Libs");
093            for (final String file2 : files) {
094                print(file2);
095            }
096        }
097
098        printBlock("Locale information");
099        print("Default locale: " + Locale.getDefault());
100        print("Default timezone: " + TimeZone.getDefault());
101
102        file = new File("config");
103        if (file.isDirectory()) {
104            final String[] files = file.list(new FilenameFilter() {
105                @Override
106                public boolean accept(final File dir, final String name) {
107                    return new File(dir, name).isFile();
108                }
109            });
110            printBlock("Config files");
111            for (final String file2 : files) {
112                print(file2);
113            }
114
115            for (final String file2 : files) {
116                print("");
117                print("--------------------------------------------------------------------------------------------------------");
118                print(file2);
119                print("");
120                LineNumberReader fileInputStream = null;
121                try {
122                    fileInputStream = new LineNumberReader(new FileReader(new File(file, file2)));
123                    String line;
124                    while ((line = fileInputStream.readLine()) != null) {
125                        print(fileInputStream.getLineNumber() + "  " + line);
126                    }
127                } catch (final Exception e) {
128                    throw new IsisException(e);
129                } finally {
130                    CloseableExtensions.closeSafely(fileInputStream);
131                }
132                print("");
133            }
134
135        }
136    }
137
138    public void printVersion() {
139        final String date = AboutIsis.getFrameworkCompileDate();
140        final String compileDate = date == null ? "" : ", compiled on " + date;
141        print(AboutIsis.getFrameworkName() + ", " + AboutIsis.getFrameworkVersion() + compileDate);
142    }
143
144    public void printErrorMessage(final String message) {
145        output.println("Error: " + message);
146    }
147
148}