1 /*-
2  * Copyright (C) 2008 Erik Larsson
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 package org.catacombae.storage.fs;
19 
20 import org.catacombae.storage.fs.hfsx.HFSXFileSystemHandlerFactory;
21 import org.catacombae.storage.fs.hfsplus.HFSPlusFileSystemHandlerFactory;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.ArrayList;
25 import java.util.LinkedList;
26 import java.util.List;
27 import org.catacombae.storage.fs.hfs.HFSFileSystemHandlerFactory;
28 
29 /**
30  * Contains the possible "major file system types" that the library will work
31  * with, and defines all file system handler implementations for these file
32  * systems that are available in the build.
33  *
34  * @author <a href="http://www.catacombae.org/" target="_top">Erik Larsson</a>
35  */
36 public enum FileSystemMajorType {
37     APPLE_MFS,
38     APPLE_HFS(HFSFileSystemHandlerFactory.class),
39     APPLE_HFS_PLUS(HFSPlusFileSystemHandlerFactory.class),
40     APPLE_HFSX(HFSXFileSystemHandlerFactory.class),
41     APPLE_UFS, APPLE_PRODOS, FAT12, FAT16, FAT32, EXFAT, NTFS, HPFS, EXT3,
42     REISERFS, REISER4, XFS, JFS, ZFS, UNKNOWN;
43 
44     /*
45     private static final Map<String, List<Class<? extends FileSystemHandlerFactory>>> DEFINED_FACTORIES;
46 
47     static {
48         DEFINED_FACTORIES =
49                 new HashMap<String, List<Class<? extends FileSystemHandlerFactory>>>();
50 
51         LinkedList<Class<? extends FileSystemHandlerFactory>> hfsPlusHandlers =
52                 new LinkedList<Class<? extends FileSystemHandlerFactory>>();
53         hfsPlusHandlers.addLast(HFSPlusFileSystemHandlerFactory.class);
54         DEFINED_FACTORIES.put("HFS+", hfsPlusHandlers);
55     }
56 
57     public static Class[] getFactories(String fsType) {
58         List<Class<? extends FileSystemHandlerFactory>> factoryList =
59                 DEFINED_FACTORIES.get(fsType);
60         return factoryList.toArray(new Class[factoryList.size()]);
61     }
62      * */
63 
64     private final LinkedList<Class<? extends FileSystemHandlerFactory>> factoryClasses =
65             new LinkedList<Class<? extends FileSystemHandlerFactory>>();
66 
67     /**
68      * Creates a FileSystemMajorType with no file system handler implementations
69      * specified.
70      */
FileSystemMajorType()71     private FileSystemMajorType() {
72     }
73 
74     /**
75      * Creates a FileSystemMajorType specifing the factory classes of the available
76      * file system handler implementations for this file system. The first argument
77      * is assumed to be the default implementation.
78      *
79      * @param pFactoryClasses
80      */
FileSystemMajorType(Class<? extends FileSystemHandlerFactory> factoryClass)81     private FileSystemMajorType(Class<? extends FileSystemHandlerFactory> factoryClass) {
82         this.factoryClasses.add(factoryClass);
83     }
84 
85     /**
86      * If an external implementor wants to register a factory class for a type,
87      * it calls this method. If there are no current factory classes tied to
88      * this type, the added class will become its default factory.
89      *
90      * @param factoryClass the factory class to register with this type.
91      */
addFactoryClass(Class<? extends FileSystemHandlerFactory> factoryClass)92     public void addFactoryClass(Class<? extends FileSystemHandlerFactory> factoryClass) {
93         this.factoryClasses.addLast(factoryClass);
94     }
95 
96     /**
97      * Returns all registered factory classes for this type. The first entry in
98      * the list will be the default factory class.
99      * @return all registered factory classes for this type.
100      */
getFactoryClasses()101     public List<Class<? extends FileSystemHandlerFactory>> getFactoryClasses() {
102         return new ArrayList<Class<? extends FileSystemHandlerFactory>>(factoryClasses);
103     }
104 
105     /**
106      * Returns a newly created factory from the type's default factory class.
107      * If there is no factory classes defined for the type, <code>null</code> is
108      * returned.
109      *
110      * @return a newly created factory from the type's default factory class.
111      */
createDefaultHandlerFactory()112     public FileSystemHandlerFactory createDefaultHandlerFactory() {
113         if(factoryClasses.size() == 0)
114             return null;
115         else {
116             Class<? extends FileSystemHandlerFactory> factoryClass =
117                     factoryClasses.getFirst();
118             return createHandlerFactory(factoryClass);
119         }
120     }
121 
122     /**
123      * Returns a newly created factory from a specified factory class.
124      *
125      * @param factoryClass the factory class of the new object.
126      * @return a newly created factory from a specified factory class.
127      */
createHandlerFactory(Class <? extends FileSystemHandlerFactory> factoryClass)128     public static FileSystemHandlerFactory createHandlerFactory(Class <? extends FileSystemHandlerFactory> factoryClass) {
129         try {
130             Constructor<? extends FileSystemHandlerFactory> c =
131                     factoryClass.getConstructor();
132             return c.newInstance();
133         } catch(NoSuchMethodException e) {
134             e.printStackTrace();
135         } catch(InstantiationException e) {
136             e.printStackTrace();
137         } catch(IllegalAccessException e) {
138             e.printStackTrace();
139         } catch(IllegalArgumentException e) {
140             e.printStackTrace();
141         } catch(InvocationTargetException e) {
142             e.printStackTrace();
143         }
144         return null;
145     }
146 }
147