View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
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  package mnemosyne.util;
17  
18  import java.io.File;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  
22  /***
23   * @version $Id: Util.java,v 1.1.1.1 2004/08/07 06:41:26 charlesblaxland Exp $
24   */
25  public class Util
26  {
27      public static final String CLONE_METHOD_NAME = "clone";
28  
29      public static boolean ensureDirectoryExists(String directoryName)
30      {
31          boolean success = true;
32          File directory = new File(directoryName);
33          if (!directory.exists())
34          {
35              success = directory.mkdirs();
36          }
37          return success;
38      }
39  
40      public static Object callCloneViaReflection(Object obj) throws CloneException
41      {
42          Object clone = null;
43          try
44          {
45              Method method = Object.class.getDeclaredMethod(CLONE_METHOD_NAME, null);
46              method.setAccessible(true);
47              clone = method.invoke(obj, null);
48          }
49          catch (NoSuchMethodException e)
50          {
51              throw new PersistenceRuntimeException(e);
52          }
53          catch (IllegalAccessException e)
54          {
55              throw new PersistenceRuntimeException(e);
56          }
57          catch (InvocationTargetException e)
58          {
59              throw new CloneException("Clone method raised exception: " + e.getMessage(), e);
60          }
61  
62          return clone;
63      }
64  }