SHOW:
|
|
- or go back to the newest paste.
| 1 | package ru.nsu.ccfit.batalov.task1; | |
| 2 | ||
| 3 | import ru.nsu.ccfit.batalov.task1.command.Command; | |
| 4 | import ru.nsu.ccfit.batalov.task1.exception.BadArgumentsException; | |
| 5 | import ru.nsu.ccfit.batalov.task1.exception.UnknownCommandException; | |
| 6 | ||
| 7 | import java.io.IOException; | |
| 8 | import java.io.InputStream; | |
| 9 | import java.lang.reflect.Constructor; | |
| 10 | import java.lang.reflect.InvocationTargetException; | |
| 11 | import java.util.HashMap; | |
| 12 | import java.util.Properties; | |
| 13 | import java.util.logging.Level; | |
| 14 | import java.util.logging.Logger; | |
| 15 | ||
| 16 | /** | |
| 17 | * Creates classes which implemented calculating. | |
| 18 | * @author batalov | |
| 19 | */ | |
| 20 | public class Factory {
| |
| 21 | private static final Logger LOGGER = Logger.getLogger(Calculator.class.getName()); | |
| 22 | private HashMap<String, Class> containerCommands = new HashMap<String, Class>(); //create informative name | |
| 23 | private Properties properties = new Properties(); | |
| 24 | ||
| 25 | /** | |
| 26 | * Configures the factory. | |
| 27 | */ | |
| 28 | private void readConfig() {
| |
| 29 | InputStream in = null; | |
| 30 | try {
| |
| 31 | in = getClass().getResourceAsStream("config.properties");
| |
| 32 | properties.load(in); | |
| 33 | //you should close the stream | |
| 34 | //closing the stream should be performed using "finally" construct | |
| 35 | } catch (IOException e) {
| |
| 36 | LOGGER.log(Level.SEVERE, "exception ", e); | |
| 37 | } | |
| 38 | finally {
| |
| 39 | if (in != null){
| |
| 40 | try {
| |
| 41 | in.close(); | |
| 42 | } catch (IOException e) {
| |
| 43 | LOGGER.log(Level.SEVERE,"exception ",e); | |
| 44 | } | |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Instances new object. | |
| 51 | * @param obj {@link java.lang.Class}
| |
| 52 | * @param arg {@link java.lang.String}
| |
| 53 | * @return {@link ru.nsu.ccfit.batalov.task1.command.Command}
| |
| 54 | * @throws IllegalAccessException {@link java.lang.IllegalAccessException} if factory cant uses method or constructors for creates object.
| |
| 55 | * @throws NoSuchMethodException {@link java.lang.NoSuchMethodException} if factory cant finds method or constructors for creates object.
| |
| 56 | * @throws InvocationTargetException {@link java.lang.reflect.InvocationTargetException} if cant call constructor.
| |
| 57 | * @throws InstantiationException {@link java.lang.InstantiationException} if smth wrong with instantiation.
| |
| 58 | */ | |
| 59 | @SuppressWarnings("unchecked")
| |
| 60 | private Command instance (Class obj , String[] arg) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
| |
| 61 | Class[] types = { String[].class };
| |
| 62 | Constructor constructor = obj.getDeclaredConstructor(types); | |
| 63 | Object[] params = { arg};
| |
| 64 | return (Command)constructor.newInstance(params); | |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Creates command for calculating. | |
| 69 | * @param commandName {@link java.lang.String}
| |
| 70 | * @param arg {@link java.lang.String}
| |
| 71 | * @return {@link ru.nsu.ccfit.batalov.task1.command.Command}
| |
| 72 | * @throws UnknownCommandException always when factory cant create class. | |
| 73 | */ | |
| 74 | public Command get(String commandName, String[] arg) throws UnknownCommandException{
| |
| 75 | if (properties.isEmpty()) {
| |
| 76 | this.readConfig(); | |
| 77 | } | |
| 78 | Command command; | |
| 79 | try{
| |
| 80 | if (containerCommands.containsKey(commandName)) {
| |
| 81 | Class obj = containerCommands.get(commandName); | |
| 82 | command = instance(obj, arg); | |
| 83 | } else if (properties.containsKey(commandName)) {
| |
| 84 | Class obj = Class.forName(properties.getProperty(commandName)); | |
| 85 | command = instance(obj, arg); | |
| 86 | containerCommands.put(commandName,obj); | |
| 87 | } else {
| |
| 88 | throw new UnknownCommandException(); | |
| 89 | } | |
| 90 | }catch (NullPointerException e ){
| |
| 91 | throw new UnknownCommandException(); | |
| 92 | }catch (InvocationTargetException e){
| |
| 93 | throw new BadArgumentsException(); | |
| 94 | }catch (ClassNotFoundException e) {
| |
| 95 | throw new UnknownCommandException(); | |
| 96 | }catch (IllegalAccessException e) {
| |
| 97 | throw new UnknownCommandException(); | |
| 98 | }catch (NoSuchMethodException e) {
| |
| 99 | throw new UnknownCommandException(); | |
| 100 | }catch (InstantiationException e) {
| |
| 101 | throw new UnknownCommandException(); | |
| 102 | } | |
| 103 | return command; | |
| 104 | } | |
| 105 | } |