EMMA Coverage Report (generated Mon Aug 23 17:21:34 CEST 2010)
[all classes][org.dcache.boot]

COVERAGE SUMMARY FOR SOURCE FILE [BootLoader.java]

nameclass, %method, %block, %line, %
BootLoader.java100% (1/1)45%  (5/11)44%  (211/480)43%  (50.3/118)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BootLoader100% (1/1)45%  (5/11)44%  (211/480)43%  (50.3/118)
BootLoader (): void 0%   (0/1)0%   (0/3)0%   (0/2)
compileToShell (Layout): void 0%   (0/1)0%   (0/68)0%   (0/17)
compileToShell (ReplaceableProperties): void 0%   (0/1)0%   (0/44)0%   (0/7)
help (): void 0%   (0/1)0%   (0/39)0%   (0/15)
quote (String): String 0%   (0/1)0%   (0/14)0%   (0/1)
quoteForCase (String): String 0%   (0/1)0%   (0/15)0%   (0/1)
main (String []): void 100% (1/1)41%  (59/145)40%  (16.3/41)
getDefaults (): ReplaceableProperties 100% (1/1)100% (25/25)100% (5/5)
loadConfiguration (ReplaceableProperties, String []): ReplaceableProperties 100% (1/1)100% (66/66)100% (10/10)
loadLayout (ReplaceableProperties): Layout 100% (1/1)100% (15/15)100% (3/3)
logToConsoleAtLevel (Level): void 100% (1/1)100% (46/46)100% (16/16)

1package org.dcache.boot;
2 
3import java.util.Collection;
4import java.util.Collections;
5import java.util.List;
6import java.util.Set;
7import java.util.ArrayList;
8import java.util.regex.Pattern;
9import java.net.UnknownHostException;
10import java.net.InetAddress;
11import java.net.URI;
12import java.net.URISyntaxException;
13 
14import java.io.IOException;
15import java.io.FileNotFoundException;
16import java.io.File;
17import java.io.PrintStream;
18 
19import dmg.util.Args;
20import dmg.util.CommandException;
21 
22import org.dcache.util.ReplaceableProperties;
23import org.dcache.util.DeprecatableProperties;
24import org.dcache.util.Glob;
25 
26import org.slf4j.LoggerFactory;
27import ch.qos.logback.classic.Logger;
28import ch.qos.logback.classic.LoggerContext;
29import ch.qos.logback.classic.Level;
30import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
31import ch.qos.logback.core.ConsoleAppender;
32import ch.qos.logback.classic.spi.ILoggingEvent;
33 
34/**
35 * Boot loader for dCache. This class contains the main method of
36 * dCache. It is responsible for parsing the configuration files and
37 * for boot strapping domains.
38 *
39 * All methods are static. The class is never instantiated.
40 */
41public class BootLoader
42{
43    private static final String PROPERTY_DCACHE_LAYOUT_URI = "dcache.layout.uri";
44    private static final String PROPERTY_HOST_NAME = "host.name";
45    private static final String PROPERTY_HOST_FQDN = "host.fqdn";
46 
47    private static final String CMD_START = "start";
48    private static final String CMD_COMPILE = "compile";
49 
50    private static final char OPT_SILENT = 'q';
51    private static final String OPT_CONFIG_FILE = "f";
52    private static final String OPT_CONFIG_FILE_DELIMITER = ":";
53 
54    private static final String CONSOLE_APPENDER_NAME = "console";
55    private static final String CONSOLE_APPENDER_PATTERN = "%-5level - %msg%n";
56 
57    private BootLoader()
58    {
59    }
60 
61    private static void help()
62    {
63        System.err.println("SYNOPSIS:");
64        System.err.println("  java org.dcache.util.BootLoader [-q] [-f=PATH[:PATH...]] COMMAND [ARGS]");
65        System.err.println();
66        System.err.println("OPTIONS:");
67        System.err.println("    -q    Suppress warnings.");
68        System.err.println("    -f    Paths to the dCache setup files.");
69        System.err.println();
70        System.err.println("COMMANDS:");
71        System.err.println("   start DOMAIN");
72        System.err.println("          Start a domain.");
73        System.err.println();
74        System.err.println("   compile");
75        System.err.println("          Compiles the layout to a shell script.");
76        System.exit(1);
77    }
78 
79    private static ReplaceableProperties getDefaults()
80        throws UnknownHostException
81    {
82        InetAddress localhost = InetAddress.getLocalHost();
83 
84        ReplaceableProperties properties =
85            new ReplaceableProperties(System.getProperties());
86        properties.setProperty(PROPERTY_HOST_NAME,
87                               localhost.getHostName().split("\\.")[0]);
88        properties.setProperty(PROPERTY_HOST_FQDN,
89                               localhost.getCanonicalHostName());
90        return properties;
91    }
92 
93    private static ReplaceableProperties
94        loadConfiguration(ReplaceableProperties config, String[] paths)
95        throws IOException
96    {
97        for (String path: paths) {
98            config = new DeprecatableProperties(config);
99            File file = new File(path);
100            if (file.isFile()) {
101                config.loadFile(file);
102            } else if (file.isDirectory()) {
103                for (File entry: file.listFiles()) {
104                    if (entry.isFile() && entry.getName().endsWith(".properties")) {
105                        config.loadFile(entry);
106                    }
107                }
108            }
109        }
110        return config;
111    }
112 
113    private static Layout loadLayout(ReplaceableProperties config)
114        throws IOException, URISyntaxException
115    {
116        Layout layout = new Layout(config);
117        layout.load(new URI(config.getReplacement(PROPERTY_DCACHE_LAYOUT_URI)));
118        return layout;
119    }
120 
121    private static String quote(String s)
122    {
123        return s.replace("\\", "\\\\").replace("$", "\\$").replace("`", "\\`").replace("\"", "\\\"");
124    }
125 
126    private static String quoteForCase(String s)
127    {
128        return quote(s).replace(")", "\\)").replace("?", "\\?").replace("*", "\\*").replace("[", "\\[");
129    }
130 
131    private static void compileToShell(ReplaceableProperties properties)
132    {
133        PrintStream out = System.out;
134        out.println("      case \"$1\" in");
135        for (String key: properties.stringPropertyNames()) {
136            out.println("        " + quoteForCase(key) + ") echo \"" + quote(properties.getReplacement(key)) + "\";;");
137        }
138        out.println("        *) undefinedProperty \"$1\" \"$2\";;");
139        out.println("      esac");
140    }
141 
142    private static void compileToShell(Layout layout)
143    {
144        PrintStream out = System.out;
145        out.println("getProperty()");
146        out.println("{");
147        out.println("  case \"$2\" in");
148        out.println("    \"\")");
149        compileToShell(layout.properties());
150        out.println("      ;;");
151        for (Domain domain: layout.getDomains()) {
152            out.println("    " + quoteForCase(domain.getName()) + ")");
153            compileToShell(domain.properties());
154            out.println("      ;;");
155        }
156        out.println("    *)");
157        out.println("      undefinedDomain \"$1\"");
158        out.println("      ;;");
159        out.println("  esac;");
160        out.println("}");
161    }
162 
163    public static void main(String arguments[])
164    {
165        try {
166            Args args = new Args(arguments);
167            if (args.argc() < 1) {
168                help();
169            }
170 
171            /* Basic logging setup that will be used until the real
172             * log configuration is loaded.
173             */
174            Level level =
175                args.isOneCharOption(OPT_SILENT) ? Level.ERROR : Level.WARN;
176            logToConsoleAtLevel(level);
177 
178            ReplaceableProperties config = getDefaults();
179            String tmp = args.getOpt(OPT_CONFIG_FILE);
180            if (tmp != null) {
181                config =
182                    loadConfiguration(config, tmp.split(OPT_CONFIG_FILE_DELIMITER));
183            }
184 
185            Layout layout = loadLayout(config);
186            String command = args.argv(0);
187            if (command.equals(CMD_START)) {
188                if (args.argc() != 2) {
189                    throw new IllegalArgumentException("Missing argument: Domain name");
190                }
191                Domain domain = layout.getDomain(args.argv(1));
192                if (domain == null) {
193                    throw new IllegalArgumentException("No such domain: " + args.argv(1));
194                }
195 
196                domain.start();
197            } else if (command.equals(CMD_COMPILE)) {
198                compileToShell(layout);
199            } else {
200                throw new IllegalArgumentException("Invalid command: " + command);
201            }
202        } catch (IllegalArgumentException e) {
203            System.err.println(e.getMessage());
204            System.exit(1);
205        } catch (FileNotFoundException e) {
206            System.err.println(e.getMessage());
207            System.exit(1);
208        } catch (IOException e) {
209            System.err.println(e.toString());
210            System.exit(1);
211        } catch (URISyntaxException e) {
212            System.err.println(e.getMessage());
213            System.exit(1);
214        } catch (CommandException e) {
215            System.err.println(e.getMessage());
216            System.exit(1);
217        } catch (RuntimeException e) {
218            e.printStackTrace();
219            System.exit(1);
220        }
221    }
222 
223    private static void logToConsoleAtLevel(Level level)
224    {
225        LoggerContext loggerContext =
226            (LoggerContext) LoggerFactory.getILoggerFactory();
227        loggerContext.reset();
228 
229        ConsoleAppender<ILoggingEvent> ca =
230            new ConsoleAppender<ILoggingEvent>();
231        ca.setTarget("System.err");
232        ca.setContext(loggerContext);
233        ca.setName(CONSOLE_APPENDER_NAME);
234        PatternLayoutEncoder pl = new PatternLayoutEncoder();
235        pl.setContext(loggerContext);
236        pl.setPattern(CONSOLE_APPENDER_PATTERN);
237        pl.start();
238 
239        ca.setEncoder(pl);
240        ca.start();
241        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
242        rootLogger.addAppender(ca);
243        rootLogger.setLevel(level);
244    }
245}

[all classes][org.dcache.boot]
EMMA 2.0.5312 (C) Vladimir Roubtsov