maven-ooo-plugin /src/main/java/org/openoffice/maven/ConfigurationManager.java

Language Java Lines 509
MD5 Hash 4316a35bcdfda91428582b144c43d3c8
Repository https://github.com/oboehm/maven-ooo-plugin.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*************************************************************************
 * $RCSfile: ConfigurationManager.java,v $
 * $Revision: 1.1 $
 * last change: $Author: cedricbosdo $ $Date: 2007/10/08 18:35:15 $
 * The Contents of this file are made available subject to the terms of
 * either of the GNU Lesser General Public License Version 2.1
 * Sun Microsystems Inc., October, 2000
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 * Copyright: 2002 by Sun Microsystems, Inc.
 * All Rights Reserved.
 * Contributor(s): Cedric Bosdonnat
 ************************************************************************/
package org.openoffice.maven;

import java.io.File;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.codehaus.plexus.util.cli.*;
import org.openoffice.maven.utils.FileFinder;

/**
 * Stores the Mojo configuration for use in the build visitors.
 * 
 * @author Cedric Bosdonnat
 */
public class ConfigurationManager {
    
    private static Log log = new SystemStreamLog();

    /**
     * Path to the URD directory in the output directory.
     */
    private static final String URD_DIR = "urd";

    /**
     * Path to the <code>types.rdb</code> file in the output directory.
     */
    private static final String TYPES_FILE = "types.rdb";

    /**
     * The path to the IDL directory in the resources directory.
     */
    private static final String IDL_DIR = "idl";

    private static File sOoo;

    private static File sSdk;
    
    private static File idlDir;

    private static File sOutput;

    private static File sClassesOutput;
    
    /**
     * We want to use the same Log stream as the Mojos in this project.
     * So you can set it here.
     * 
     * @param mojoLog the new Log
     */
    public static void setLog(Log mojoLog) {
        log = mojoLog;
    }
    
    /**
     * Gets the log.
     *
     * @return the log
     */
    public static Log getLog() {
        return log;
    }

    /**
     * @return the folder where OpenOffice.org is installed.
     */
    public static File getOOo() {
        if (sOoo == null) {
            sOoo = Environment.getOfficeHome();
        }
        return sOoo;
    }
    
    /**
     * Sets the OpenOffice.org installation folder to use for the build.
     * 
     * @param pOoo
     *            the OpenOffice.org installation folder.
     */
    public static void setOOo(File pOoo) {
        assert pOoo != null;
        sOoo = pOoo;
        Environment.setOfficeHome(pOoo);
    }
    
    /**
     * The office home attribute is initialized with the given dir parameter if
     * it is set.
     *
     * @param dir the init value for office home
     * @return the office home directory
     */
    public static File initOOo(final File dir) {
        if (dir != null) {
            setOOo(dir);
        }
        return getOOo();
    }

    /**
     * @return the OpenOffice.org <code>types.rdb</code> file path
     */
    public static String getOOoTypesFile() {
        File oooTypes = FileFinder.tryFiles(new File(getOOo(), "/program/types.rdb"),
                new File(Environment.getOoSdkUreHome(), "/share/misc/types.rdb"),
                new File(Environment.getOoSdkUreHome(), "/misc/types.rdb"));
        if (oooTypes == null) {
            throw new RuntimeException("types.rdb not found");
        }
        return oooTypes.getPath();
    }

    /**
     * @return the OpenOffice.org <code>offapi.rdb</code> file path
     */
    public static String getOffapiTypesFile() {
        return new File(Environment.getOfficeBaseHome(), "program/offapi.rdb").getPath();
    }
    
    /**
     * @return the folder where OpenOffice.org SDK is installed.
     */
    public static File getSdk() {
        if (sSdk == null) {
            sSdk = Environment.getOoSdkHome();
        }
        return sSdk;
    }

    /**
     * @return the folder where the classes files are generated.
     */
    public static File getClassesOutput() {
        if (!sClassesOutput.exists()) {
            sClassesOutput.mkdirs();
        }
        return sClassesOutput;
    }

    /**
     * @return the path to the folder where URD files should be generated
     */
    public static String getUrdDir() {
        return new File(sOutput, URD_DIR).getPath();
    }

    /**
     * @return the path to the generated <code>types.rdb</code>.
     */
    public static String getTypesFile() {
        return new File(sOutput, TYPES_FILE).getPath();
    }

    /**
     * Sets the idl dir.
     *
     * @param dir the new idl dir
     */
    public static synchronized void setIdlDir(File dir) {
        idlDir = dir;
    }
    
    /**
     * @return the path to the folder containing the IDL files to build or
     *         <code>null</code> if no IDL folder has been found.
     */
    public static synchronized File getIdlDir() {
        if (idlDir == null) {
            File dir = new File("src/main/", IDL_DIR);
            if (dir.isDirectory()) {
                idlDir = dir;
            }
        }
        return idlDir;
    }

    /**
     * Sets the OpenOffice.org SDK installation folder to use for the build.
     * 
     * @param pSdk
     *            the OpenOffice.org SDK installation folder.
     */
    public static void setSdk(File pSdk) {
        assert pSdk != null;
        sSdk = pSdk;
        Environment.setOoSdkHome(pSdk);
    }
    
    /**
     * The SDK attribute is initialized with the given dir parameter if
     * it is set.
     *
     * @param dir the init value for the SDK
     * @return the SDK directory
     */
    public static File initSdk(final File dir) {
        if (dir != null) {
            setSdk(dir);
        }
        return getSdk();
    }

    /**
     * Sets the directory where the generated files should go.
     * 
     * @param pOutput
     *            the output directory.
     */
    public static void setOutput(File pOutput) {
        sOutput = pOutput;
    }
    
    /**
     * Gets the output.
     *
     * @return the output
     */
    public static File getOutput() {
        return sOutput;
    }

    /**
     * Sets the directory where the generated classes should go.
     * 
     * @param pOutputDirectory
     *            the classes output directory.
     */
    public static void setClassesOutput(File pOutputDirectory) {
        sClassesOutput = pOutputDirectory;
    }

//    @Deprecated
//    public static Process runTool(String pCommand) throws Exception {
//        return runTool(new String[] { pCommand });
//    }
//
//    @Deprecated
//    public static Process runTool(String pCommand, String args) throws Exception {
//        return runTool(new String[] { pCommand, args });
//    }
//
//    @Deprecated
//    public static Process runTool(String[] pCommand) throws Exception {
//
//        String os = System.getProperty("os.name").toLowerCase();
//        String pathSep = System.getProperty("path.separator");
//
//        String[] env = new String[0];
//        String[] cmd = new String[2 + pCommand.length];
//        File oooLibs;
//
//        String sdkBin = getSdkBinPath();
//        String oooBin = getOOoBinPath();
//
//        if (os.startsWith("windows")) {
//            // Windows environment
//            env = new String[1];
//            oooLibs = new File(getOOo(), "/program");
//            env[0] = "PATH=C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;" 
//                    + sdkBin + pathSep + Environment.getOoSdkUreBinDir() + pathSep
//                    + oooBin + pathSep + oooLibs.getCanonicalPath();
//            if (os.startsWith("windows 9")) {
//                cmd[0] = "command.com";
//            } else {
//                cmd[0] = "cmd.exe";
//            }
//            cmd[1] = "/C";
//            System.arraycopy(pCommand, 0, cmd, 2, pCommand.length);
//        } else if (SystemUtils.IS_OS_MAC) {
//            // MacOS environment
//            env = new String[2];
//            oooLibs = Environment.getOoSdkUreLibDir();
//            env[0] = "PATH=" + sdkBin + pathSep + oooBin + pathSep + Environment.getOoSdkUreBinDir();
//            env[1] = "DYLD_LIBRARY_PATH=" + oooLibs.getCanonicalPath();
//            cmd = getCmd4Unix(pCommand);
//        } else {
//            // *NIX environment
//            env = new String[2];
//            oooLibs = new File(getOOo(), "/program");
//            env[0] = "PATH=" + sdkBin + pathSep + oooBin + pathSep + Environment.getOoSdkUreBinDir();
//            env[1] = "LD_LIBRARY_PATH=" + oooLibs.getCanonicalPath();
//            cmd = getCmd4Unix(pCommand);
//        }
//
//
//        // TODO: proper way of doing it according to
//        // http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook
//        //
//        // Commandline cl = new Commandline("command");
//        // cl.addArguments( new String[] { "arg1", "arg2", "arg3" } );
//        // StreamConsumer output = new CommandLineUtils.StringStreamConsumer();
//        // StreamConsumer error = new CommandLineUtils.StringStreamConsumer();
//        // int returnValue = CommandLineUtils.executeCommandLine(cl, output,
//        // error);
//
//        // Create the process
//        ProcessBuilder b = new ProcessBuilder(cmd);
//        // copy pasted from ProcessBuilder.environment(String[])
//        Map<String, String> e = b.environment();
//        for (String envstring : env) {
//            if (envstring.indexOf('\u0000') != -1)
//                envstring = envstring.replaceFirst("\u0000.*", "");
//
//            int eqlsign = envstring.indexOf('=', 1);
//            if (eqlsign != -1) {
//                String key = envstring.substring(0, eqlsign);
//                String value = envstring.substring(eqlsign + 1);
//                if ("path".equalsIgnoreCase(key)) {
//                    if (os.startsWith("windows")) {
//                        // for windows, path env var is not case sensitive.
//                        for (String k : e.keySet()) {
//                            if ("path".equalsIgnoreCase(k)) {
//                                key = k;
//                                break;
//                            }
//                        }
//                    }
//                    e.put(key, e.get(key) + pathSep + value);
//                } else {
//                    e.put(key, value);
//                }
//            }
//        }
//        b.redirectErrorStream(true);
//
//         log.debug("\nRunning: [" + StringUtils.join(cmd, " ") +
//         "] \nwith env [" + StringUtils.join(env, " ") + "]");
//
//        Process process = b.start();
//        check(process, cmd[2]);
//        return process;
//    }
//    
//    private static void check(Process process, String command) throws InterruptedException, Exception {
//        ErrorReader.readErrors(process.getErrorStream());
//        int status = process.waitFor();
//        if (status != 0) {
//            InputStream istream = process.getInputStream();
//            String msg = IOUtils.toString(istream);
//            throw new Exception("RC=" + status + ": " + command + "\n" + msg);
//        }
//    }
//    
//    private static String[] getCmd4Unix(String[] args) {
//        String[] cmd = new String[3];
//        cmd[0] = "sh";
//        cmd[1] = "-c";
//        cmd[2] = args[0];
//        for (int i = 1; i < args.length; i++) {
//            cmd[2] += " " + args[i];
//        }
//        return cmd;
//    }

    /**
     * Returns the path to the SDK binaries depending on the OS and the
     * architecture.
     * 
     * @param pHome
     *            the OpenOffice.org SDK home
     * @return the full path to the SDK binaries
     */
    private static String getSdkBinPath() {
        File sdkHome = Environment.getOoSdkHome();
        // OOo SDK does not seem to include the target os in their packaging
        // anymore. Tested with 3.2.0
        String path = "/bin";
        if (new File(sdkHome, path).exists()) {
            return new File(sdkHome, path).getPath();
        }

        // Get the Architecture properties
        String arch = System.getProperty("os.arch").toLowerCase();

        if (SystemUtils.IS_OS_WINDOWS) {
            path = "/windows/bin/";
        } else if (SystemUtils.IS_OS_SOLARIS) {
            if (arch.equals("sparc")) {
                path = "/solsparc/bin";
            } else {
                path = "/solintel/bin";
            }
        } else if (SystemUtils.IS_OS_MAC) {
            path = "/bin";
        } else {
            path = "/linux/bin";
        }

        return new File(sdkHome, path).getPath();
    }
    
    /**
     * Returns the path to the OOO binaries depending on the OS and the
     * architecture.
     * 
     * @return the full path to the OOo binaries
     */
    private static String getOOoBinPath() {
        File oooHome = Environment.getOfficeHome();
        File binDir = new File(oooHome, "program");
        if (SystemUtils.IS_OS_MAC) {
            binDir = new File(oooHome, "Contents/MacOS");
        }
        return binDir.getAbsolutePath();
    }
    
    /**
     * Run command.
     * See {@link "http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook"}.
     *
     * @param cmd the command
     * @return the exit code of the command
     * @throws CommandLineException the command line exception
     */
    public static int runCommand(final String cmd) throws CommandLineException {
        Commandline cl = new Commandline(cmd);
        return runCommand(cl);
    }

    /**
     * Run command.
     * See {@link "http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook"}.
     *
     * @param cmd the command
     * @param args the args
     * @return the exit code of the command
     * @throws CommandLineException the command line exception
     */
    public static int runCommand(final String cmd, final String... args) throws CommandLineException {
        Commandline cl = new Commandline(cmd);
        cl.addArguments(args);
        return runCommand(cl);
    }

    private static int runCommand(Commandline cl) throws CommandLineException {
        try {
            setUpEnvironmentFor(cl);
        } catch (Exception e) {
            log.warn("can't setup environment for '" + cl + "' - will try without environment...");
        }
        CommandLineUtils.StringStreamConsumer output = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer error = new CommandLineUtils.StringStreamConsumer();
        int returnValue = CommandLineUtils.executeCommandLine(cl, output, error);
        String outmsg = output.getOutput().trim();
        if (StringUtils.isNotEmpty(outmsg)) {
            log.info(outmsg);
        }
        String errmsg = error.getOutput().trim();
        if (StringUtils.isNotEmpty(errmsg)) {
            log.warn(errmsg);
        }
        log.info("'" + cl + "' returned with " + returnValue);
        return returnValue;
    }
    
    private static void setUpEnvironmentFor(final Commandline cl) throws Exception {
        cl.addSystemEnvironment();
        Properties envVars = cl.getSystemEnvVars();
        String path = envVars.getProperty("PATH", "");
        String pathSep = System.getProperty("path.separator", ":");
        path = getSdkBinPath() + pathSep + getOOoBinPath() + pathSep + Environment.getOoSdkUreBinDir() + pathSep + path;        
        cl.addEnvironment("PATH", path);
        //log.debug("PATH=" + path);
        String oooLibs = Environment.getOoSdkUreLibDir().getCanonicalPath();
        if (SystemUtils.IS_OS_WINDOWS) {
            // I'm not sure if this works / is necessary
            cl.addEnvironment("DLLPATH", oooLibs);
            //log.debug("DLLPATH=" + oooLibs);
        } else if (SystemUtils.IS_OS_MAC) {
            cl.addEnvironment("DYLD_LIBRARY_PATH", oooLibs);
            //log.debug("DYLD_LIBRARY_PATH=" + oooLibs);
        } else {
            // *NIX environment
            cl.addEnvironment("LD_LIBRARY_PATH", oooLibs);
            //log.debug("LD_LIBRARY_PATH=" + oooLibs);
        }
    }
    
}
Back to Top