001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Sep 17, 2007
014: * @author Will Gorman
015: */
016: package org.pentaho.plugin.versionchecker;
017:
018: import java.lang.reflect.Method;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.logging.Log;
023: import org.dom4j.Document;
024: import org.dom4j.Element;
025: import org.pentaho.core.util.XmlHelper;
026: import org.pentaho.messages.Messages;
027:
028: public class PentahoVersionCheckReflectHelper {
029:
030: public static boolean isVersionCheckerAvailable() {
031: try {
032: Class.forName("org.pentaho.versionchecker.VersionChecker"); //$NON-NLS-1$
033: return true;
034: } catch (ClassNotFoundException e) {
035: // ignore
036: }
037: return false;
038: }
039:
040: public static List performVersionCheck(
041: boolean ignoreExistingUpdates, int versionRequestFlags) {
042: // check to see if jar is loaded before continuing
043: if (isVersionCheckerAvailable()) {
044: try {
045:
046: // use reflection so anyone can delete the version checker jar without pain
047:
048: // PentahoVersionCheckHelper helper = new PentahoVersionCheckHelper();
049: Class helperClass = Class
050: .forName("org.pentaho.plugin.versionchecker.PentahoVersionCheckHelper"); //$NON-NLS-1$
051: Object helper = helperClass.getConstructors()[0]
052: .newInstance(new Object[] {});
053:
054: // helper.setIgnoreExistingUpdates(ignoreExistingUpdates);
055: Method setIgnoreExistingUpdatesMethod = helperClass
056: .getDeclaredMethod(
057: "setIgnoreExistingUpdates", new Class[] { Boolean.TYPE }); //$NON-NLS-1$
058: setIgnoreExistingUpdatesMethod.invoke(helper,
059: new Object[] { new Boolean(
060: ignoreExistingUpdates) });
061:
062: // helper.setVersionRequestFlags(versionRequestFlags);
063: Method setVersionRequestFlagsMethod = helperClass
064: .getDeclaredMethod(
065: "setVersionRequestFlags", new Class[] { Integer.TYPE }); //$NON-NLS-1$
066: setVersionRequestFlagsMethod
067: .invoke(helper, new Object[] { new Integer(
068: versionRequestFlags) });
069:
070: // helper.performUpdate();
071: Method performUpdateMethod = helperClass
072: .getDeclaredMethod(
073: "performUpdate", new Class[] {}); //$NON-NLS-1$
074: performUpdateMethod.invoke(helper, new Object[] {});
075:
076: // List results = helper.getResults();
077: Method getResultsMethod = helperClass
078: .getDeclaredMethod("getResults", new Class[] {}); //$NON-NLS-1$
079: List results = (List) getResultsMethod.invoke(helper,
080: new Object[] {});
081: return results;
082:
083: } catch (Exception e) {
084: // ignore errors
085: }
086: }
087:
088: return null;
089: }
090:
091: public static String logVersionCheck(List results, Log logger) {
092: String output = null;
093: if (results != null && results.size() > 0) {
094: String result = results.get(0).toString();
095: try {
096: Document doc = XmlHelper.getDocFromString(result);
097: if (doc != null) {
098: List nodes = doc.selectNodes("//update"); //$NON-NLS-1$
099: Iterator nodeIter = nodes.iterator();
100: while (nodeIter.hasNext()) {
101: Element updateElement = (Element) nodeIter
102: .next();
103:
104: String title = updateElement
105: .attributeValue("title"); //$NON-NLS-1$
106: String version = updateElement
107: .attributeValue("version"); //$NON-NLS-1$
108: String type = updateElement
109: .attributeValue("type"); //$NON-NLS-1$
110: String downloadurl = XmlHelper.getNodeText(
111: "downloadurl", updateElement); //$NON-NLS-1$
112: if (downloadurl != null) {
113: downloadurl = downloadurl.trim();
114: }
115: logger
116: .info(Messages
117: .getString(
118: "VersionCheck.UPDATE_MESSAGE", title, version, type, downloadurl)); //$NON-NLS-1$
119: }
120:
121: nodes = doc.selectNodes("//error"); //$NON-NLS-1$
122: nodeIter = nodes.iterator();
123: while (nodeIter.hasNext()) {
124: Element errorElement = (Element) nodeIter
125: .next();
126: String message = errorElement.getText();
127: logger.info(Messages.getString(
128: "VersionCheck.ERROR_MESSAGE", message)); //$NON-NLS-1$
129: }
130: }
131: output = result;
132: } catch (Exception e) {
133: output = "<vercheck><error><[!CDATA[" + Messages.getString("VersionCheck.ERROR_MESSAGE", e.getMessage()) + "]]></error></vercheck>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
134: }
135: } else {
136: output = "<vercheck><error><[!CDATA[" + Messages.getString("VersionCheck.NO_RESULT_MESSAGE") + "]]></error></vercheck>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
137: }
138: return output;
139: }
140: }
|