| 1 | /* |
| 2 | * $Id: ComClassPathBuilder.java,v 1.3 2006/02/01 18:54:35 adamp Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | package com.moesol.bindings.platform_sdk.component_services; |
| 13 | |
| 14 | import java.io.File; |
| 15 | import java.net.MalformedURLException; |
| 16 | import java.net.URL; |
| 17 | import java.util.SortedMap; |
| 18 | import java.util.TreeMap; |
| 19 | import java.util.logging.Level; |
| 20 | import java.util.logging.Logger; |
| 21 | import java.util.prefs.BackingStoreException; |
| 22 | import java.util.prefs.Preferences; |
| 23 | |
| 24 | /** |
| 25 | * Build a class path array suitable for URLClassLoader from the system |
| 26 | * preferences and user preferences. If a component is installed only for the |
| 27 | * current user then these classpath values will only be in the user |
| 28 | * preferences, otherwise they will be in the system preferences. Here, we |
| 29 | * search both. |
| 30 | * |
| 31 | * @author Hastings |
| 32 | */ |
| 33 | public class ComClassPathBuilder { |
| 34 | private final Preferences m_sys; |
| 35 | private final Preferences m_user; |
| 36 | |
| 37 | private static Logger s_logger = Logger.getLogger(ComClassPathBuilder.class.getName()); |
| 38 | |
| 39 | public ComClassPathBuilder(String group_name) { |
| 40 | m_sys = systemNodeForGroup(group_name); |
| 41 | m_user = userNodeForGroup(group_name); |
| 42 | } |
| 43 | |
| 44 | public static Preferences systemNodeForGroup(String group_name) { |
| 45 | Preferences sys = Preferences.systemNodeForPackage(COM.class); |
| 46 | return findNode(sys, group_name); |
| 47 | } |
| 48 | |
| 49 | public static Preferences userNodeForGroup(String group_name) { |
| 50 | Preferences sys = Preferences.userNodeForPackage(COM.class); |
| 51 | return findNode(sys, group_name); |
| 52 | } |
| 53 | |
| 54 | private static Preferences findNode(Preferences pkg_root, String group_name) { |
| 55 | Preferences in_proc = pkg_root.node("com_inproc_groups"); |
| 56 | Preferences group = in_proc.node(group_name); |
| 57 | Preferences classpath = group.node("classpath"); |
| 58 | return classpath; |
| 59 | } |
| 60 | |
| 61 | public URL[] buildClassPath() throws BackingStoreException, MalformedURLException { |
| 62 | SortedMap map = new TreeMap(); |
| 63 | addPaths(map, m_sys); |
| 64 | addPaths(map, m_user); |
| 65 | return (URL[])map.values().toArray(new URL[0]); |
| 66 | } |
| 67 | |
| 68 | private void addPaths(SortedMap map, Preferences node) throws BackingStoreException, MalformedURLException { |
| 69 | String[] keys = node.keys(); |
| 70 | for (int i = 0; i < keys.length; i++) { |
| 71 | if (s_logger.isLoggable(Level.FINE)) { |
| 72 | s_logger.log(Level.FINE, "Adding Path: " + keys[i] + " value: " + node.get(keys[i], null)); |
| 73 | } |
| 74 | File f = new File(node.get(keys[i], null)); |
| 75 | URL url = f.toURL(); |
| 76 | map.put(keys[i], url); |
| 77 | } |
| 78 | } |
| 79 | } |