package com.onaro.sanscreen.client.help; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.Properties; import javax.help.HelpBroker; import javax.help.HelpSet; import javax.help.HelpSetException; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * This class provides a HelpSet and HelpContextProvider based on classloader and path to a help module's files. * * @author rnoel * */ public class HelpModule implements IHelpModule { private static Logger logger = LogManager.getLogger(HelpModule.class); // Name of the help set file. private static final String HELP_SET_FILE_NAME = "JavaHelp.hs"; //$NON-NLS-1$ private final HelpSet helpSet; private HelpBroker helpBroker; private final HelpContextProvider helpContextProvider; /** * Constructor. * * @param loader the class loader we'll use to find the help resources * @param path the path to the root of the help directory containing the HelpSet. */ public HelpModule(ClassLoader loader, String helpPath) throws HelpSetException { ClassLoader helpSetLoader; String path = helpPath; boolean isHelpPathEmpty = StringUtils.isEmpty(helpPath); // If the helpPath does not end with a "/" then treat it as a jar entry and create // a classloader for it. if(!isHelpPathEmpty && helpPath.charAt(helpPath.length() - 1) != '/') { try { String urlStr = HelpManager.SERVER_BASE_URL_STR + HelpManager.SANSCREEN_HELP_ROOT_PATH + "/" + helpPath; //$NON-NLS-1$ URL url = new URL(urlStr); helpSetLoader = new URLClassLoader(new URL[] { url } ); // Since we are loading from a jar file we don't want the path path = null; } catch(MalformedURLException mue) { logger.warn("Warning; exception creating a classloader helpPath '" + helpPath + "': " + mue.toString()); //$NON-NLS-1$ //$NON-NLS-2$ // Try the loader that loads from the directory. Not likely to work but we will handle not finding // the helpset when we try loading it. helpSetLoader = loader; } } else { // Just use our passed in loader helpSetLoader = loader; } helpSet = getHelpSet(helpSetLoader, path); helpBroker = helpSet.createHelpBroker(); helpContextProvider = new HelpContextProvider(helpSetLoader, path); helpSet.setHomeID(HelpManager.SANSCREEN_ROOT_CONTEXT); logger.debug("Found help module: " + (isHelpPathEmpty ? "MASTER" : helpPath)); //$NON-NLS-1$ //$NON-NLS-2$ } /** * Get the help set, given a path and classloader. * * @param loader the class loader we'll use to find the help resources * @param helpSetPath the path to the HelpSet file resource * @return the HelpSet we found. */ private HelpSet getHelpSet(ClassLoader loader, String helpPath) throws HelpSetException { try { // If helpPath is null then we want the master hs or we are loading from a jar so don't prepend "/". String path = (StringUtils.isEmpty(helpPath) ? StringUtils.EMPTY : helpPath + "/"); //$NON-NLS-1$ URL url = HelpSet.findHelpSet(loader, path + HELP_SET_FILE_NAME); return (new HelpSet(loader, url)); } catch (HelpSetException e) { String error = "Help Set '" + helpPath + "' not found!"; //$NON-NLS-1$ //$NON-NLS-2$ logger.error(error); throw e; } } public HelpSet getHelpSet() { return helpSet; } public HelpBroker getHelpBroker() { return helpBroker; } public HelpContextProvider getHelpContextProvider() { return helpContextProvider; } // TEMP TEST CODE. DO NOT RELEASE!!! public static void main (String[] args) { ClassLoader loader; try { String urlStr = HelpManager.SERVER_BASE_URL_STR + HelpManager.SANSCREEN_HELP_ROOT_PATH + "/" + "SANscreen/client/si/JavaHelp.zip"; //$NON-NLS-1$ //$NON-NLS-2$ URL url = new URL(urlStr); loader = new URLClassLoader(new URL[] { url } ); String propsFilePath = "contexts.properties"; //$NON-NLS-1$ InputStream inStream = loader.getResourceAsStream(propsFilePath); if(inStream == null) { System.out.println("Context mapping file '" + propsFilePath + "' not found."); //$NON-NLS-1$ //$NON-NLS-2$ return; } try { Properties props = new Properties(); props.load(inStream); System.out.println(props); } catch(IOException ioe) { ioe.printStackTrace(); } finally { try { inStream.close(); } catch(IOException ioe) {} } } catch(MalformedURLException mue) { mue.printStackTrace(); } } }