Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.configuration » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   package com.sshtools.daemon.configuration;
   27   
   28   import com.sshtools.daemon.vfs;
   29   
   30   import org.apache.commons.logging;
   31   
   32   import org.xml.sax;
   33   import org.xml.sax.helpers;
   34   
   35   import java.io;
   36   
   37   import java.util;
   38   
   39   import javax.xml.parsers;
   40   
   41   
   42   /**
   43    *
   44    *
   45    * @author $author$
   46    * @version $Revision: 1.13 $
   47    */
   48   public class PlatformConfiguration extends DefaultHandler {
   49       private static Log log = LogFactory.getLog(PlatformConfiguration.class);
   50       private static final String PLATFORM_ELEMENT = "PlatformConfiguration";
   51       private static final String NATIVE_PROCESS_ELEMENT = "NativeProcessProvider";
   52       private static final String NATIVE_AUTH_ELEMENT = "NativeAuthenticationProvider";
   53       private static final String NFS_ELEMENT = "NativeFileSystemProvider";
   54       private static final String NATIVE_SETTING_ELEMENT = "NativeSetting";
   55       private static final String VFSMOUNT_ELEMENT = "VFSMount";
   56       private static final String VFSROOT_ELEMENT = "VFSRoot";
   57       private static final String VFSPERMISSION_ELEMENT = "VFSPermission";
   58       private static final String PATH_ATTRIBUTE = "path";
   59       private static final String MOUNT_ATTRIBUTE = "mount";
   60       private static final String NAME_ATTRIBUTE = "name";
   61       private static final String VALUE_ATTRIBUTE = "value";
   62       private static final String PERMISSIONS_ATTRIBUTE = "permissions";
   63       private String currentElement = null;
   64       private Map nativeSettings = new HashMap();
   65       private String nativeProcessProvider = null;
   66       private String nativeAuthenticationProvider = null;
   67       private String nativeFileSystemProvider = null;
   68       private Map vfsMounts = new HashMap();
   69       private VFSMount vfsRoot = null;
   70   
   71       /**
   72    * Creates a new PlatformConfiguration object.
   73    *
   74    * @param in
   75    *
   76    * @throws SAXException
   77    * @throws ParserConfigurationException
   78    * @throws IOException
   79    */
   80       protected PlatformConfiguration(InputStream in)
   81           throws SAXException, ParserConfigurationException, IOException {
   82           reload(in);
   83       }
   84   
   85       /**
   86    *
   87    *
   88    * @param in
   89    *
   90    * @throws SAXException
   91    * @throws ParserConfigurationException
   92    * @throws IOException
   93    */
   94       public void reload(InputStream in)
   95           throws SAXException, ParserConfigurationException, IOException {
   96           SAXParserFactory saxFactory = SAXParserFactory.newInstance();
   97           SAXParser saxParser = saxFactory.newSAXParser();
   98           saxParser.parse(in, new PlatformConfigurationSAXHandler());
   99       }
  100   
  101       /**
  102    *
  103    *
  104    * @return
  105    */
  106       public Map getVFSMounts() {
  107           return vfsMounts;
  108       }
  109   
  110       /**
  111    *
  112    *
  113    * @return
  114    */
  115       public String getNativeAuthenticationProvider() {
  116           return nativeAuthenticationProvider;
  117       }
  118   
  119       /**
  120    *
  121    *
  122    * @return
  123    */
  124       public String getNativeFileSystemProvider() {
  125           return nativeFileSystemProvider;
  126       }
  127   
  128       /**
  129    *
  130    *
  131    * @return
  132    */
  133       public String getNativeProcessProvider() {
  134           return nativeProcessProvider;
  135       }
  136   
  137       /**
  138    *
  139    *
  140    * @param name
  141    *
  142    * @return
  143    */
  144       public String getSetting(String name) {
  145           return (String) nativeSettings.get(name);
  146       }
  147   
  148       /**
  149    *
  150    *
  151    * @param name
  152    * @param defaultValue
  153    *
  154    * @return
  155    */
  156       public String getSetting(String name, String defaultValue) {
  157           if (nativeSettings.containsKey(name)) {
  158               return (String) nativeSettings.get(name);
  159           } else {
  160               return defaultValue;
  161           }
  162       }
  163   
  164       /**
  165    *
  166    *
  167    * @param name
  168    *
  169    * @return
  170    */
  171       public boolean containsSetting(String name) {
  172           return nativeSettings.containsKey(name);
  173       }
  174   
  175       /**
  176    *
  177    *
  178    * @return
  179    */
  180       public VFSMount getVFSRoot() {
  181           return vfsRoot;
  182       }
  183   
  184       /**
  185    *
  186    *
  187    * @return
  188    */
  189       public String toString() {
  190           String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  191           xml += ("<!-- Platform Configuration file, Determines the behaviour of platform specific services -->\n<" +
  192           PLATFORM_ELEMENT + ">\n");
  193           xml += "   <!-- The process provider for executing and redirecting a process -->\n";
  194           xml += ("   <" + NATIVE_PROCESS_ELEMENT + ">" + nativeProcessProvider +
  195           "</" + NATIVE_PROCESS_ELEMENT + ">\n");
  196           xml += "   <!-- The authentication provider for authenticating users and obtaining user information -->\n";
  197           xml += ("   <" + NATIVE_AUTH_ELEMENT + ">" +
  198           nativeAuthenticationProvider + "</" + NATIVE_AUTH_ELEMENT + ">\n");
  199           xml += "   <!-- Native settings which may be used by the process or authentication provider -->\n";
  200   
  201           Map.Entry entry;
  202           Iterator it = nativeSettings.entrySet().iterator();
  203   
  204           while (it.hasNext()) {
  205               entry = (Map.Entry) it.next();
  206               xml += ("   " + "<" + NATIVE_SETTING_ELEMENT + " " +
  207               NAME_ATTRIBUTE + "=\"" + entry.getKey().toString() + "\" " +
  208               VALUE_ATTRIBUTE + "=\"" + entry.getValue().toString() + "\"/>\n");
  209           }
  210   
  211           if (vfsRoot != null) {
  212               xml += ("   " + "<" + VFSROOT_ELEMENT + " path=\"" + vfsRoot +
  213               "\"/>\n");
  214           }
  215   
  216           it = vfsMounts.entrySet().iterator();
  217   
  218           String path;
  219           String mount;
  220   
  221           while (it.hasNext()) {
  222               entry = (Map.Entry) it.next();
  223               path = (String) entry.getValue();
  224               mount = (String) entry.getKey();
  225               xml += ("   " + "<" + VFSMOUNT_ELEMENT + " " +
  226               (mount.equals(path) ? ""
  227                                   : (MOUNT_ATTRIBUTE + "=\"" +
  228               entry.getKey().toString() + "\" ")) + PATH_ATTRIBUTE + "=\"" +
  229               entry.getValue().toString() + "\"/>\n");
  230           }
  231   
  232           xml += ("</" + PLATFORM_ELEMENT + ">");
  233   
  234           return xml;
  235       }
  236   
  237       class PlatformConfigurationSAXHandler extends DefaultHandler {
  238           private VFSMount currentMount = null;
  239   
  240           public void startElement(String uri, String localName, String qname,
  241               Attributes attrs) throws SAXException {
  242               if (currentElement == null) {
  243                   if (qname.equals(PLATFORM_ELEMENT)) {
  244                       currentElement = qname;
  245                   }
  246   
  247                   nativeProcessProvider = null;
  248                   nativeAuthenticationProvider = null;
  249                   nativeSettings.clear();
  250               } else {
  251                   if (currentElement.equals(PLATFORM_ELEMENT)) {
  252                       if (!qname.equals(NATIVE_PROCESS_ELEMENT) &&
  253                               !qname.equals(NATIVE_AUTH_ELEMENT) &&
  254                               !qname.equals(NATIVE_SETTING_ELEMENT) &&
  255                               !qname.equals(VFSMOUNT_ELEMENT) &&
  256                               !qname.equals(VFSROOT_ELEMENT) &&
  257                               !qname.equals(NFS_ELEMENT)) {
  258                           throw new SAXException("Unexpected element " + qname);
  259                       }
  260                   } else if (currentElement.equals(VFSMOUNT_ELEMENT)) {
  261                       if (!qname.equals(VFSPERMISSION_ELEMENT)) {
  262                           throw new SAXException("Unexpected element " + qname);
  263                       }
  264                   } else {
  265                       throw new SAXException("Unexpected element " + qname);
  266                   }
  267   
  268                   currentElement = qname;
  269   
  270                   if (qname.equals(NATIVE_SETTING_ELEMENT)) {
  271                       String name = attrs.getValue(NAME_ATTRIBUTE);
  272                       String value = attrs.getValue(VALUE_ATTRIBUTE);
  273   
  274                       if ((name == null) || (value == null)) {
  275                           throw new SAXException(
  276                               "Required attributes missing for NativeSetting element");
  277                       }
  278   
  279                       log.debug("NativeSetting " + name + "=" + value);
  280                       nativeSettings.put(name, value);
  281                   }
  282   
  283                   if (qname.equals(VFSPERMISSION_ELEMENT)) {
  284                       String name = attrs.getValue(NAME_ATTRIBUTE);
  285                       String permissions = attrs.getValue(PERMISSIONS_ATTRIBUTE);
  286                       currentMount.setPermissions(new VFSPermission(name,
  287                               permissions));
  288                   }
  289   
  290                   if (qname.equals(VFSMOUNT_ELEMENT)) {
  291                       String path = attrs.getValue(PATH_ATTRIBUTE);
  292                       String mount = attrs.getValue(MOUNT_ATTRIBUTE);
  293                       String permissions = attrs.getValue(PERMISSIONS_ATTRIBUTE);
  294   
  295                       if ((path != null) && (mount != null)) {
  296                           // verify the mount - must start with / and be unique
  297                           if (!mount.trim().equals("/")) {
  298                               try {
  299                                   currentMount = new VFSMount(mount, path);
  300   
  301                                   if (permissions == null) {
  302                                       currentMount.setPermissions(new VFSPermission(
  303                                               "default"));
  304                                   } else {
  305                                       currentMount.setPermissions(new VFSPermission(
  306                                               "default", permissions));
  307                                   }
  308   
  309                                   if (!vfsMounts.containsKey(
  310                                               currentMount.getMount())) {
  311                                       vfsMounts.put(currentMount.getMount(),
  312                                           currentMount);
  313                                   } else {
  314                                       throw new SAXException("The mount " +
  315                                           mount + " is already defined");
  316                                   }
  317                               } catch (IOException ex1) {
  318                                   throw new SAXException(
  319                                       "VFSMount element is invalid mount=" +
  320                                       mount + " path=" + path);
  321                               }
  322                           } else {
  323                               throw new SAXException(
  324                                   "The root mount / cannot be configured, use <VFSRoot path=\"" +
  325                                   path + "\"/> instead");
  326                           }
  327                       } else {
  328                           throw new SAXException("Required " + PATH_ATTRIBUTE +
  329                               " attribute for element <" + VFSMOUNT_ELEMENT +
  330                               "> is missing");
  331                       }
  332                   }
  333   
  334                   if (qname.equals(VFSROOT_ELEMENT)) {
  335                       if (vfsRoot != null) {
  336                           throw new SAXException(
  337                               "Only one VFSRoot can be defined");
  338                       }
  339   
  340                       String path = attrs.getValue(PATH_ATTRIBUTE);
  341                       String permissions = attrs.getValue(PERMISSIONS_ATTRIBUTE);
  342   
  343                       try {
  344                           vfsRoot = new VFSMount("/", path);
  345   
  346                           if (permissions != null) {
  347                               vfsRoot.setPermissions(new VFSPermission(
  348                                       "default", permissions));
  349                           } else {
  350                               vfsRoot.setPermissions(new VFSPermission("default"));
  351                           }
  352   
  353                           vfsRoot.setRoot(true);
  354                       } catch (IOException ex) {
  355                           throw new SAXException(
  356                               "VFSRoot element is invalid path=" + path);
  357                       }
  358                   }
  359               }
  360           }
  361   
  362           public void characters(char[] ch, int start, int length)
  363               throws SAXException {
  364               if (currentElement == null) {
  365                   throw new SAXException("Unexpected characters found");
  366               }
  367   
  368               if (currentElement.equals(NATIVE_AUTH_ELEMENT)) {
  369                   nativeAuthenticationProvider = new String(ch, start, length).trim();
  370                   log.debug("NativeAuthenticationProvider=" +
  371                       nativeAuthenticationProvider);
  372   
  373                   return;
  374               }
  375   
  376               if (currentElement.equals(NATIVE_PROCESS_ELEMENT)) {
  377                   nativeProcessProvider = new String(ch, start, length).trim();
  378                   log.debug("NativeProcessProvider=" + nativeProcessProvider);
  379   
  380                   return;
  381               }
  382   
  383               if (currentElement.equals(NFS_ELEMENT)) {
  384                   nativeFileSystemProvider = new String(ch, start, length).trim();
  385                   log.debug("NativeFileSystemProvider=" +
  386                       nativeFileSystemProvider);
  387   
  388                   return;
  389               }
  390           }
  391   
  392           public void endElement(String uri, String localName, String qname)
  393               throws SAXException {
  394               if (currentElement == null) {
  395                   throw new SAXException("Unexpected end element for " + qname);
  396               }
  397   
  398               if (!currentElement.equals(qname)) {
  399                   throw new SAXException("Unexpected end element found");
  400               }
  401   
  402               if (currentElement.equals(PLATFORM_ELEMENT)) {
  403                   currentElement = null;
  404   
  405                   return;
  406               }
  407   
  408               if (currentElement.equals(VFSPERMISSION_ELEMENT)) {
  409                   currentElement = VFSMOUNT_ELEMENT;
  410               } else if (!currentElement.equals(NATIVE_SETTING_ELEMENT) &&
  411                       !currentElement.equals(NATIVE_AUTH_ELEMENT) &&
  412                       !currentElement.equals(NATIVE_PROCESS_ELEMENT) &&
  413                       !currentElement.equals(NFS_ELEMENT) &&
  414                       !currentElement.equals(VFSMOUNT_ELEMENT) &&
  415                       !currentElement.equals(VFSROOT_ELEMENT)) {
  416                   throw new SAXException("Unexpected end element for " + qname);
  417               } else {
  418                   currentElement = PLATFORM_ELEMENT;
  419               }
  420           }
  421       }
  422   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.configuration » [javadoc | source]