PageRenderTime 83ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/openoffice/maven/ConfigurationManager.java

https://github.com/oboehm/maven-ooo-plugin
Java | 508 lines | 180 code | 41 blank | 287 comment | 32 complexity | 4316a35bcdfda91428582b144c43d3c8 MD5 | raw file
  1. /*************************************************************************
  2. * $RCSfile: ConfigurationManager.java,v $
  3. * $Revision: 1.1 $
  4. * last change: $Author: cedricbosdo $ $Date: 2007/10/08 18:35:15 $
  5. * The Contents of this file are made available subject to the terms of
  6. * either of the GNU Lesser General Public License Version 2.1
  7. * Sun Microsystems Inc., October, 2000
  8. * GNU Lesser General Public License Version 2.1
  9. * =============================================
  10. * Copyright 2000 by Sun Microsystems, Inc.
  11. * 901 San Antonio Road, Palo Alto, CA 94303, USA
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License version 2.1, as published by the Free Software Foundation.
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
  24. * Copyright: 2002 by Sun Microsystems, Inc.
  25. * All Rights Reserved.
  26. * Contributor(s): Cedric Bosdonnat
  27. ************************************************************************/
  28. package org.openoffice.maven;
  29. import java.io.File;
  30. import java.util.Properties;
  31. import org.apache.commons.lang.StringUtils;
  32. import org.apache.commons.lang.SystemUtils;
  33. import org.apache.maven.plugin.logging.Log;
  34. import org.apache.maven.plugin.logging.SystemStreamLog;
  35. import org.codehaus.plexus.util.cli.*;
  36. import org.openoffice.maven.utils.FileFinder;
  37. /**
  38. * Stores the Mojo configuration for use in the build visitors.
  39. *
  40. * @author Cedric Bosdonnat
  41. */
  42. public class ConfigurationManager {
  43. private static Log log = new SystemStreamLog();
  44. /**
  45. * Path to the URD directory in the output directory.
  46. */
  47. private static final String URD_DIR = "urd";
  48. /**
  49. * Path to the <code>types.rdb</code> file in the output directory.
  50. */
  51. private static final String TYPES_FILE = "types.rdb";
  52. /**
  53. * The path to the IDL directory in the resources directory.
  54. */
  55. private static final String IDL_DIR = "idl";
  56. private static File sOoo;
  57. private static File sSdk;
  58. private static File idlDir;
  59. private static File sOutput;
  60. private static File sClassesOutput;
  61. /**
  62. * We want to use the same Log stream as the Mojos in this project.
  63. * So you can set it here.
  64. *
  65. * @param mojoLog the new Log
  66. */
  67. public static void setLog(Log mojoLog) {
  68. log = mojoLog;
  69. }
  70. /**
  71. * Gets the log.
  72. *
  73. * @return the log
  74. */
  75. public static Log getLog() {
  76. return log;
  77. }
  78. /**
  79. * @return the folder where OpenOffice.org is installed.
  80. */
  81. public static File getOOo() {
  82. if (sOoo == null) {
  83. sOoo = Environment.getOfficeHome();
  84. }
  85. return sOoo;
  86. }
  87. /**
  88. * Sets the OpenOffice.org installation folder to use for the build.
  89. *
  90. * @param pOoo
  91. * the OpenOffice.org installation folder.
  92. */
  93. public static void setOOo(File pOoo) {
  94. assert pOoo != null;
  95. sOoo = pOoo;
  96. Environment.setOfficeHome(pOoo);
  97. }
  98. /**
  99. * The office home attribute is initialized with the given dir parameter if
  100. * it is set.
  101. *
  102. * @param dir the init value for office home
  103. * @return the office home directory
  104. */
  105. public static File initOOo(final File dir) {
  106. if (dir != null) {
  107. setOOo(dir);
  108. }
  109. return getOOo();
  110. }
  111. /**
  112. * @return the OpenOffice.org <code>types.rdb</code> file path
  113. */
  114. public static String getOOoTypesFile() {
  115. File oooTypes = FileFinder.tryFiles(new File(getOOo(), "/program/types.rdb"),
  116. new File(Environment.getOoSdkUreHome(), "/share/misc/types.rdb"),
  117. new File(Environment.getOoSdkUreHome(), "/misc/types.rdb"));
  118. if (oooTypes == null) {
  119. throw new RuntimeException("types.rdb not found");
  120. }
  121. return oooTypes.getPath();
  122. }
  123. /**
  124. * @return the OpenOffice.org <code>offapi.rdb</code> file path
  125. */
  126. public static String getOffapiTypesFile() {
  127. return new File(Environment.getOfficeBaseHome(), "program/offapi.rdb").getPath();
  128. }
  129. /**
  130. * @return the folder where OpenOffice.org SDK is installed.
  131. */
  132. public static File getSdk() {
  133. if (sSdk == null) {
  134. sSdk = Environment.getOoSdkHome();
  135. }
  136. return sSdk;
  137. }
  138. /**
  139. * @return the folder where the classes files are generated.
  140. */
  141. public static File getClassesOutput() {
  142. if (!sClassesOutput.exists()) {
  143. sClassesOutput.mkdirs();
  144. }
  145. return sClassesOutput;
  146. }
  147. /**
  148. * @return the path to the folder where URD files should be generated
  149. */
  150. public static String getUrdDir() {
  151. return new File(sOutput, URD_DIR).getPath();
  152. }
  153. /**
  154. * @return the path to the generated <code>types.rdb</code>.
  155. */
  156. public static String getTypesFile() {
  157. return new File(sOutput, TYPES_FILE).getPath();
  158. }
  159. /**
  160. * Sets the idl dir.
  161. *
  162. * @param dir the new idl dir
  163. */
  164. public static synchronized void setIdlDir(File dir) {
  165. idlDir = dir;
  166. }
  167. /**
  168. * @return the path to the folder containing the IDL files to build or
  169. * <code>null</code> if no IDL folder has been found.
  170. */
  171. public static synchronized File getIdlDir() {
  172. if (idlDir == null) {
  173. File dir = new File("src/main/", IDL_DIR);
  174. if (dir.isDirectory()) {
  175. idlDir = dir;
  176. }
  177. }
  178. return idlDir;
  179. }
  180. /**
  181. * Sets the OpenOffice.org SDK installation folder to use for the build.
  182. *
  183. * @param pSdk
  184. * the OpenOffice.org SDK installation folder.
  185. */
  186. public static void setSdk(File pSdk) {
  187. assert pSdk != null;
  188. sSdk = pSdk;
  189. Environment.setOoSdkHome(pSdk);
  190. }
  191. /**
  192. * The SDK attribute is initialized with the given dir parameter if
  193. * it is set.
  194. *
  195. * @param dir the init value for the SDK
  196. * @return the SDK directory
  197. */
  198. public static File initSdk(final File dir) {
  199. if (dir != null) {
  200. setSdk(dir);
  201. }
  202. return getSdk();
  203. }
  204. /**
  205. * Sets the directory where the generated files should go.
  206. *
  207. * @param pOutput
  208. * the output directory.
  209. */
  210. public static void setOutput(File pOutput) {
  211. sOutput = pOutput;
  212. }
  213. /**
  214. * Gets the output.
  215. *
  216. * @return the output
  217. */
  218. public static File getOutput() {
  219. return sOutput;
  220. }
  221. /**
  222. * Sets the directory where the generated classes should go.
  223. *
  224. * @param pOutputDirectory
  225. * the classes output directory.
  226. */
  227. public static void setClassesOutput(File pOutputDirectory) {
  228. sClassesOutput = pOutputDirectory;
  229. }
  230. // @Deprecated
  231. // public static Process runTool(String pCommand) throws Exception {
  232. // return runTool(new String[] { pCommand });
  233. // }
  234. //
  235. // @Deprecated
  236. // public static Process runTool(String pCommand, String args) throws Exception {
  237. // return runTool(new String[] { pCommand, args });
  238. // }
  239. //
  240. // @Deprecated
  241. // public static Process runTool(String[] pCommand) throws Exception {
  242. //
  243. // String os = System.getProperty("os.name").toLowerCase();
  244. // String pathSep = System.getProperty("path.separator");
  245. //
  246. // String[] env = new String[0];
  247. // String[] cmd = new String[2 + pCommand.length];
  248. // File oooLibs;
  249. //
  250. // String sdkBin = getSdkBinPath();
  251. // String oooBin = getOOoBinPath();
  252. //
  253. // if (os.startsWith("windows")) {
  254. // // Windows environment
  255. // env = new String[1];
  256. // oooLibs = new File(getOOo(), "/program");
  257. // env[0] = "PATH=C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"
  258. // + sdkBin + pathSep + Environment.getOoSdkUreBinDir() + pathSep
  259. // + oooBin + pathSep + oooLibs.getCanonicalPath();
  260. // if (os.startsWith("windows 9")) {
  261. // cmd[0] = "command.com";
  262. // } else {
  263. // cmd[0] = "cmd.exe";
  264. // }
  265. // cmd[1] = "/C";
  266. // System.arraycopy(pCommand, 0, cmd, 2, pCommand.length);
  267. // } else if (SystemUtils.IS_OS_MAC) {
  268. // // MacOS environment
  269. // env = new String[2];
  270. // oooLibs = Environment.getOoSdkUreLibDir();
  271. // env[0] = "PATH=" + sdkBin + pathSep + oooBin + pathSep + Environment.getOoSdkUreBinDir();
  272. // env[1] = "DYLD_LIBRARY_PATH=" + oooLibs.getCanonicalPath();
  273. // cmd = getCmd4Unix(pCommand);
  274. // } else {
  275. // // *NIX environment
  276. // env = new String[2];
  277. // oooLibs = new File(getOOo(), "/program");
  278. // env[0] = "PATH=" + sdkBin + pathSep + oooBin + pathSep + Environment.getOoSdkUreBinDir();
  279. // env[1] = "LD_LIBRARY_PATH=" + oooLibs.getCanonicalPath();
  280. // cmd = getCmd4Unix(pCommand);
  281. // }
  282. //
  283. //
  284. // // TODO: proper way of doing it according to
  285. // // http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook
  286. // //
  287. // // Commandline cl = new Commandline("command");
  288. // // cl.addArguments( new String[] { "arg1", "arg2", "arg3" } );
  289. // // StreamConsumer output = new CommandLineUtils.StringStreamConsumer();
  290. // // StreamConsumer error = new CommandLineUtils.StringStreamConsumer();
  291. // // int returnValue = CommandLineUtils.executeCommandLine(cl, output,
  292. // // error);
  293. //
  294. // // Create the process
  295. // ProcessBuilder b = new ProcessBuilder(cmd);
  296. // // copy pasted from ProcessBuilder.environment(String[])
  297. // Map<String, String> e = b.environment();
  298. // for (String envstring : env) {
  299. // if (envstring.indexOf('\u0000') != -1)
  300. // envstring = envstring.replaceFirst("\u0000.*", "");
  301. //
  302. // int eqlsign = envstring.indexOf('=', 1);
  303. // if (eqlsign != -1) {
  304. // String key = envstring.substring(0, eqlsign);
  305. // String value = envstring.substring(eqlsign + 1);
  306. // if ("path".equalsIgnoreCase(key)) {
  307. // if (os.startsWith("windows")) {
  308. // // for windows, path env var is not case sensitive.
  309. // for (String k : e.keySet()) {
  310. // if ("path".equalsIgnoreCase(k)) {
  311. // key = k;
  312. // break;
  313. // }
  314. // }
  315. // }
  316. // e.put(key, e.get(key) + pathSep + value);
  317. // } else {
  318. // e.put(key, value);
  319. // }
  320. // }
  321. // }
  322. // b.redirectErrorStream(true);
  323. //
  324. // log.debug("\nRunning: [" + StringUtils.join(cmd, " ") +
  325. // "] \nwith env [" + StringUtils.join(env, " ") + "]");
  326. //
  327. // Process process = b.start();
  328. // check(process, cmd[2]);
  329. // return process;
  330. // }
  331. //
  332. // private static void check(Process process, String command) throws InterruptedException, Exception {
  333. // ErrorReader.readErrors(process.getErrorStream());
  334. // int status = process.waitFor();
  335. // if (status != 0) {
  336. // InputStream istream = process.getInputStream();
  337. // String msg = IOUtils.toString(istream);
  338. // throw new Exception("RC=" + status + ": " + command + "\n" + msg);
  339. // }
  340. // }
  341. //
  342. // private static String[] getCmd4Unix(String[] args) {
  343. // String[] cmd = new String[3];
  344. // cmd[0] = "sh";
  345. // cmd[1] = "-c";
  346. // cmd[2] = args[0];
  347. // for (int i = 1; i < args.length; i++) {
  348. // cmd[2] += " " + args[i];
  349. // }
  350. // return cmd;
  351. // }
  352. /**
  353. * Returns the path to the SDK binaries depending on the OS and the
  354. * architecture.
  355. *
  356. * @param pHome
  357. * the OpenOffice.org SDK home
  358. * @return the full path to the SDK binaries
  359. */
  360. private static String getSdkBinPath() {
  361. File sdkHome = Environment.getOoSdkHome();
  362. // OOo SDK does not seem to include the target os in their packaging
  363. // anymore. Tested with 3.2.0
  364. String path = "/bin";
  365. if (new File(sdkHome, path).exists()) {
  366. return new File(sdkHome, path).getPath();
  367. }
  368. // Get the Architecture properties
  369. String arch = System.getProperty("os.arch").toLowerCase();
  370. if (SystemUtils.IS_OS_WINDOWS) {
  371. path = "/windows/bin/";
  372. } else if (SystemUtils.IS_OS_SOLARIS) {
  373. if (arch.equals("sparc")) {
  374. path = "/solsparc/bin";
  375. } else {
  376. path = "/solintel/bin";
  377. }
  378. } else if (SystemUtils.IS_OS_MAC) {
  379. path = "/bin";
  380. } else {
  381. path = "/linux/bin";
  382. }
  383. return new File(sdkHome, path).getPath();
  384. }
  385. /**
  386. * Returns the path to the OOO binaries depending on the OS and the
  387. * architecture.
  388. *
  389. * @return the full path to the OOo binaries
  390. */
  391. private static String getOOoBinPath() {
  392. File oooHome = Environment.getOfficeHome();
  393. File binDir = new File(oooHome, "program");
  394. if (SystemUtils.IS_OS_MAC) {
  395. binDir = new File(oooHome, "Contents/MacOS");
  396. }
  397. return binDir.getAbsolutePath();
  398. }
  399. /**
  400. * Run command.
  401. * See {@link "http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook"}.
  402. *
  403. * @param cmd the command
  404. * @return the exit code of the command
  405. * @throws CommandLineException the command line exception
  406. */
  407. public static int runCommand(final String cmd) throws CommandLineException {
  408. Commandline cl = new Commandline(cmd);
  409. return runCommand(cl);
  410. }
  411. /**
  412. * Run command.
  413. * See {@link "http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook"}.
  414. *
  415. * @param cmd the command
  416. * @param args the args
  417. * @return the exit code of the command
  418. * @throws CommandLineException the command line exception
  419. */
  420. public static int runCommand(final String cmd, final String... args) throws CommandLineException {
  421. Commandline cl = new Commandline(cmd);
  422. cl.addArguments(args);
  423. return runCommand(cl);
  424. }
  425. private static int runCommand(Commandline cl) throws CommandLineException {
  426. try {
  427. setUpEnvironmentFor(cl);
  428. } catch (Exception e) {
  429. log.warn("can't setup environment for '" + cl + "' - will try without environment...");
  430. }
  431. CommandLineUtils.StringStreamConsumer output = new CommandLineUtils.StringStreamConsumer();
  432. CommandLineUtils.StringStreamConsumer error = new CommandLineUtils.StringStreamConsumer();
  433. int returnValue = CommandLineUtils.executeCommandLine(cl, output, error);
  434. String outmsg = output.getOutput().trim();
  435. if (StringUtils.isNotEmpty(outmsg)) {
  436. log.info(outmsg);
  437. }
  438. String errmsg = error.getOutput().trim();
  439. if (StringUtils.isNotEmpty(errmsg)) {
  440. log.warn(errmsg);
  441. }
  442. log.info("'" + cl + "' returned with " + returnValue);
  443. return returnValue;
  444. }
  445. private static void setUpEnvironmentFor(final Commandline cl) throws Exception {
  446. cl.addSystemEnvironment();
  447. Properties envVars = cl.getSystemEnvVars();
  448. String path = envVars.getProperty("PATH", "");
  449. String pathSep = System.getProperty("path.separator", ":");
  450. path = getSdkBinPath() + pathSep + getOOoBinPath() + pathSep + Environment.getOoSdkUreBinDir() + pathSep + path;
  451. cl.addEnvironment("PATH", path);
  452. //log.debug("PATH=" + path);
  453. String oooLibs = Environment.getOoSdkUreLibDir().getCanonicalPath();
  454. if (SystemUtils.IS_OS_WINDOWS) {
  455. // I'm not sure if this works / is necessary
  456. cl.addEnvironment("DLLPATH", oooLibs);
  457. //log.debug("DLLPATH=" + oooLibs);
  458. } else if (SystemUtils.IS_OS_MAC) {
  459. cl.addEnvironment("DYLD_LIBRARY_PATH", oooLibs);
  460. //log.debug("DYLD_LIBRARY_PATH=" + oooLibs);
  461. } else {
  462. // *NIX environment
  463. cl.addEnvironment("LD_LIBRARY_PATH", oooLibs);
  464. //log.debug("LD_LIBRARY_PATH=" + oooLibs);
  465. }
  466. }
  467. }