JhlClientAdapterFactory.java.svn-base
Upload User: liminzq
Upload Date: 2017-11-13
Package Size: 15143k
Code Size: 12k
Category:

Jsp/Servlet

Development Platform:

Java

  1. /*******************************************************************************
  2.  * Copyright (c) 2004, 2006 svnClientAdapter project and others.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  * Contributors:
  17.  *     svnClientAdapter project committers - initial API and implementation
  18.  ******************************************************************************/
  19. package org.tigris.subversion.svnclientadapter.javahl;
  20. import org.tigris.subversion.javahl.SVNClient;
  21. import org.tigris.subversion.javahl.SVNClientInterface;
  22. import org.tigris.subversion.javahl.Version;
  23. import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
  24. import org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory;
  25. import org.tigris.subversion.svnclientadapter.SVNClientException;
  26. /**
  27.  * Concrete implementation of SVNClientAdapterFactory for javahl interface.
  28.  * To register this factory, just call {@link JhlClientAdapterFactory#setup()} 
  29.  */
  30. public class JhlClientAdapterFactory extends SVNClientAdapterFactory {
  31.     
  32.     private static boolean availabilityCached = false;
  33.     private static boolean available;
  34. private static StringBuffer javaHLErrors = new StringBuffer("Failed to load JavaHL Library.nThese are the errors that were encountered:n");
  35. /** Client adapter implementation identifier */
  36.     public static final String JAVAHL_CLIENT = "javahl";
  37. /**
  38.  * Private constructor.
  39.  * Clients are expected the use {@link #createSVNClientImpl()}, res.
  40.  * ask the {@link SVNClientAdapterFactory}
  41.  */
  42.     private JhlClientAdapterFactory() {
  43.      super();
  44.     }
  45. /* (non-Javadoc)
  46.  * @see org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory#createSVNClientImpl()
  47.  */
  48. protected ISVNClientAdapter createSVNClientImpl() {
  49. return new JhlClientAdapter();
  50. }
  51.     /* (non-Javadoc)
  52.      * @see org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory#getClientType()
  53.      */
  54.     protected String getClientType() {
  55.         return JAVAHL_CLIENT;
  56.     }
  57.     
  58.     /**
  59.      * Setup the client adapter implementation and register it in the adapters factory
  60.      * @throws SVNClientException
  61.      */
  62.     public static void setup() throws SVNClientException {
  63.         if (!isAvailable()) {
  64.          throw new SVNClientException("Javahl client adapter is not available");
  65.         }
  66.         
  67.      SVNClientAdapterFactory.registerAdapterFactory(new JhlClientAdapterFactory());
  68.     }
  69.     
  70.     public static boolean isAvailable() {
  71.      if (!availabilityCached) {
  72.      Class c = null;
  73.      try {
  74.      // load a JavaHL class to see if it is found.  Do not use SVNClient as
  75.      // it will try to load native libraries and we do not want that yet
  76.      c = Class.forName("org.tigris.subversion.javahl.ClientException");
  77.      if (c == null)
  78.      return false;
  79.      } catch (Throwable t) {
  80.      availabilityCached = true;
  81.      return false;
  82.      }
  83.      // if library is already loaded, it will not be reloaded
  84.      //workaround to solve Subclipse ISSUE #83
  85.      // we will ignore these exceptions to handle scenarios where
  86.      // javaHL was built diffently.  Ultimately, if javaHL fails to load
  87.      // because of a problem in one of these libraries the proper behavior
  88.      // will still occur -- meaning JavaHL adapter is disabled.
  89.      if(isOsWindows()) {
  90.      StringBuffer bdbErrors = new StringBuffer();
  91.      boolean bdbLoaded = false;
  92. //     try {
  93. //     System.loadLibrary("libapr");
  94. //     } catch (Exception e) {
  95. //     javaHLErrors.append(e.getMessage()).append("n");
  96. //     } catch (UnsatisfiedLinkError e) {
  97. //     javaHLErrors.append(e.getMessage()).append("n");
  98. //     }
  99.      try {
  100.      System.loadLibrary("libapr-1");
  101.      } catch (Exception e) {
  102.      javaHLErrors.append(e.getMessage()).append("n");
  103.      } catch (UnsatisfiedLinkError e) {
  104.      javaHLErrors.append(e.getMessage()).append("n");
  105.      }
  106.      try {
  107.      System.loadLibrary("libapriconv-1");
  108.      } catch (Exception e) {
  109.      javaHLErrors.append(e.getMessage()).append("n");
  110.      } catch (UnsatisfiedLinkError e) {
  111.      javaHLErrors.append(e.getMessage()).append("n");
  112.      }
  113.      try {
  114.      System.loadLibrary("libeay32");
  115.      } catch (Exception e) {
  116.      javaHLErrors.append(e.getMessage()).append("n");
  117.      } catch (UnsatisfiedLinkError e) {
  118.      javaHLErrors.append(e.getMessage()).append("n");
  119.      }
  120.      try {
  121.      System.loadLibrary("libdb44");
  122.      bdbLoaded = true;
  123.      } catch (Exception e) {
  124.      bdbErrors.append(e.getMessage()).append("n");
  125.      } catch (UnsatisfiedLinkError e) {
  126.      bdbErrors.append(e.getMessage()).append("n");
  127.      }
  128. //     try {
  129. //     System.loadLibrary("libdb43");
  130. //     bdbLoaded = true;
  131. //     } catch (Exception e) {
  132. //     bdbErrors.append(e.getMessage()).append("n");
  133. //     } catch (UnsatisfiedLinkError e) {
  134. //     bdbErrors.append(e.getMessage()).append("n");
  135. //     }
  136.      if (!bdbLoaded) {
  137.      javaHLErrors.append(bdbErrors.toString());
  138.      }
  139.      try {
  140.      System.loadLibrary("ssleay32");
  141.      } catch (Exception e) {
  142.      javaHLErrors.append(e.getMessage()).append("n");
  143.      } catch (UnsatisfiedLinkError e) {
  144.      javaHLErrors.append(e.getMessage()).append("n");
  145.      }
  146. //     try {
  147. //     System.loadLibrary("libaprutil");
  148. //     } catch (Exception e) {
  149. //     javaHLErrors.append(e.getMessage()).append("n");
  150. //     } catch (UnsatisfiedLinkError e) {
  151. //     javaHLErrors.append(e.getMessage()).append("n");
  152. //     }
  153.      try {
  154.      System.loadLibrary("libaprutil-1");
  155.      } catch (Exception e) {
  156.      javaHLErrors.append(e.getMessage()).append("n");
  157.      } catch (UnsatisfiedLinkError e) {
  158.      javaHLErrors.append(e.getMessage()).append("n");
  159.      }
  160.      try {
  161.      System.loadLibrary("intl3_svn");
  162.      } catch (Exception e) {
  163.      javaHLErrors.append(e.getMessage()).append("n");
  164.      } catch (UnsatisfiedLinkError e) {
  165.      javaHLErrors.append(e.getMessage()).append("n");
  166.      }
  167.      try {
  168.      System.loadLibrary("dbghelp");
  169.      } catch (Exception e) {
  170.      javaHLErrors.append(e.getMessage()).append("n");
  171.      } catch (UnsatisfiedLinkError e) {
  172.      javaHLErrors.append(e.getMessage()).append("n");
  173.      }
  174.      try {
  175.      System.loadLibrary("libsasl");
  176.      } catch (Exception e) {
  177.      javaHLErrors.append(e.getMessage()).append("n");
  178.      } catch (UnsatisfiedLinkError e) {
  179.      javaHLErrors.append(e.getMessage()).append("n");
  180.      }
  181.             // Load DLL's for Subversion libraries -- as of 1.5    
  182.      try {
  183.      System.loadLibrary("libsvn_subr-1");
  184.      } catch (Exception e) {
  185.      javaHLErrors.append(e.getMessage()).append("n");
  186.      } catch (UnsatisfiedLinkError e) {
  187.      javaHLErrors.append(e.getMessage()).append("n");
  188.      }
  189.      try {
  190.      System.loadLibrary("libsvn_delta-1");
  191.      } catch (Exception e) {
  192.      javaHLErrors.append(e.getMessage()).append("n");
  193.      } catch (UnsatisfiedLinkError e) {
  194.      javaHLErrors.append(e.getMessage()).append("n");
  195.      }
  196.      try {
  197.      System.loadLibrary("libsvn_diff-1");
  198.      } catch (Exception e) {
  199.      javaHLErrors.append(e.getMessage()).append("n");
  200.      } catch (UnsatisfiedLinkError e) {
  201.      javaHLErrors.append(e.getMessage()).append("n");
  202.      }
  203.      try {
  204.      System.loadLibrary("libsvn_wc-1");
  205.      } catch (Exception e) {
  206.      javaHLErrors.append(e.getMessage()).append("n");
  207.      } catch (UnsatisfiedLinkError e) {
  208.      javaHLErrors.append(e.getMessage()).append("n");
  209.      }
  210.      try {
  211.      System.loadLibrary("libsvn_fs-1");
  212.      } catch (Exception e) {
  213.      javaHLErrors.append(e.getMessage()).append("n");
  214.      } catch (UnsatisfiedLinkError e) {
  215.      javaHLErrors.append(e.getMessage()).append("n");
  216.      }
  217.      try {
  218.      System.loadLibrary("libsvn_repos-1");
  219.      } catch (Exception e) {
  220.      javaHLErrors.append(e.getMessage()).append("n");
  221.      } catch (UnsatisfiedLinkError e) {
  222.      javaHLErrors.append(e.getMessage()).append("n");
  223.      }
  224.      try {
  225.      System.loadLibrary("libsvn_ra-1");
  226.      } catch (Exception e) {
  227.      javaHLErrors.append(e.getMessage()).append("n");
  228.      } catch (UnsatisfiedLinkError e) {
  229.      javaHLErrors.append(e.getMessage()).append("n");
  230.      }
  231.      try {
  232.      System.loadLibrary("libsvn_client-1");
  233.      } catch (Exception e) {
  234.      javaHLErrors.append(e.getMessage()).append("n");
  235.      } catch (UnsatisfiedLinkError e) {
  236.      javaHLErrors.append(e.getMessage()).append("n");
  237.      }
  238.      }
  239.      //workaround to solve Subclipse ISSUE #83
  240.      available = false;
  241.      try {
  242.      /*
  243.       * see if the user has specified the fully qualified path to the native
  244.       * library
  245.       */
  246.      try
  247.      {
  248.      String specifiedLibraryName =
  249.      System.getProperty("subversion.native.library");
  250.      if(specifiedLibraryName != null) {
  251.      System.load(specifiedLibraryName);
  252.      available = true;
  253.      }
  254.      }
  255.      catch(UnsatisfiedLinkError ex)
  256.      {
  257.      javaHLErrors.append(ex.getMessage()).append("n");
  258.      }
  259.      if (!available) {
  260.      /*
  261.       * first try to load the library by the new name.
  262.       * if that fails, try to load the library by the old name.
  263.       */
  264.      try
  265.      {
  266.      System.loadLibrary("libsvnjavahl-1");
  267.      }
  268.      catch(UnsatisfiedLinkError ex)
  269.      {
  270.      javaHLErrors.append(ex.getMessage() + "n");
  271.      try
  272.      {
  273.      System.loadLibrary("svnjavahl-1");
  274.      }
  275.      catch (UnsatisfiedLinkError e)
  276.      {
  277.      javaHLErrors.append(e.getMessage()).append("n");
  278.      System.loadLibrary("svnjavahl");
  279.      }
  280.      }
  281.      available = true;
  282.      }
  283.      } catch (Exception e) {
  284.      available = false;
  285.      javaHLErrors.append(e.getMessage()).append("n");
  286.      } catch (UnsatisfiedLinkError e) {
  287.      available = false;
  288.      javaHLErrors.append(e.getMessage()).append("n");
  289.      } finally {
  290.      availabilityCached = true;
  291.      }
  292.      if (!available) {
  293.      String libraryPath = System.getProperty("java.library.path");
  294.      if (libraryPath != null)
  295.      javaHLErrors.append("java.library.path = " + libraryPath);
  296.      // System.out.println(javaHLErrors.toString());
  297.      } else {
  298.      // At this point, the library appears to be available, but
  299.      // it could be too old version of JavaHL library.  We have to try
  300.      // to get the version of the library to be sure.
  301.      try {
  302.                 SVNClientInterface svnClient = new SVNClient();
  303.      Version version = svnClient.getVersion();
  304.      if (version.getMajor() == 1 && version.getMinor() == 6)
  305.      available = true;
  306.      else {
  307.      available = false;
  308.      javaHLErrors = new StringBuffer("Incompatible JavaHL library loaded.  1.6.x or later required.");
  309.      }
  310.      } catch (UnsatisfiedLinkError e) {
  311.      available = false;
  312.      javaHLErrors = new StringBuffer("Incompatible JavaHL library loaded.  1.6.x or later required.");
  313.      }
  314.      }
  315.      }
  316.      return available;
  317.     }
  318.     
  319. /**
  320.  * Answer whether running on Windows OS.
  321.  * (Actual code extracted from org.apache.commons.lang.SystemUtils.IS_OS_WINDOWS)
  322.  * (For such one simple method it does make sense to introduce dependency on whole commons-lang.jar)
  323.  * @return true when the underlying 
  324.  */
  325. public static boolean isOsWindows()
  326. {
  327.         try {
  328.             return System.getProperty("os.name").startsWith("Windows");
  329.         } catch (SecurityException ex) {
  330.             // we are not allowed to look at this property
  331.             return false;
  332.         }
  333. }
  334.     /**
  335.      * @return an error string describing problems during loading platform native libraries (if any)
  336.      */
  337.     public static String getLibraryLoadErrors() {
  338.         if (isAvailable())
  339.             return "";
  340.         else
  341.             return javaHLErrors.toString();
  342.     }
  343. }