1 /*
2 * Telescope.java
3 *
4 * Copyright (c) 2003, Raben Systems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * Neither the name of Raben Systems, Inc. nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 package com.raben.telescope.comm;
35
36 import java.util.Properties;
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.FileInputStream;
40
41
42 /***
43 * Factory for creating instance of a concrete implementation of the
44 * TelescopeInterface
45 * @author Vern Raben
46 * @version $Revision: 1.3 $ $Date: 2003/09/11 15:14:40 $
47 */
48 public class TelescopeFactory {
49
50
51 /*** Static telescope instance */
52 private static TelescopeInterface instance;
53
54 /*** Definition for CelestronNexstar class name */
55 private static final String CELESTRON_CLASS_NAME =
56 "com.raben.telescope.comm.CelestronTelescope";
57
58 /*** Definition for MeadeLx200 class name */
59 private static final String MEADE_CLASS_NAME =
60 "com.raben.telescope.comm.MeadeTelescope";
61
62
63 /*** Prevent instantiation by making constructor private */
64 private TelescopeFactory() {
65 }
66
67 /***
68 * Create instance of TelescopeInterface for telescope model that has
69 * been stored in a properties file. If properties file doesn't exist
70 * the default will be a Celestron Nextar GPS
71 * @return TelescopeInterface
72 */
73 public static TelescopeInterface newInstance() {
74 Properties telescopeProperties = loadProperties();
75 String modelName = telescopeProperties.getProperty(ScopeKey.MODEL,
76 TelescopeModel.CELESTRON_NEXSTAR_GPS.toString());
77 getInstance(modelName);
78 instance.setProperties(telescopeProperties);
79 return instance;
80 }
81
82
83 /***
84 * Get instance of TelescopeInterface for specified modelName
85 * @param modelName Name of telescope model @see TelescopeModel
86 * @return TelescopeInterface - May return null if modelName is incorrect
87 */
88 public static TelescopeInterface getInstance(String modelName) {
89 TelescopeModel model = TelescopeModel.fromString(modelName);
90 return getInstance(model);
91 }
92
93 /***
94 * Get instance of TelescopeInterface for specified telescope model
95 * @param model of telescope
96 * @return TelescopeInterface
97 */
98 public static TelescopeInterface getInstance(TelescopeModel model) {
99 String name = "";
100
101 if ((TelescopeModel.CELESTRON.equals(model))
102 || (TelescopeModel.CELESTRON_ASC.equals(model))
103 || (TelescopeModel.CELESTRON_CGE.equals(model))
104 || (TelescopeModel.CELESTRON_NEXSTAR_5I_8I.equals(model))
105 || (TelescopeModel.CELESTRON_NEXSTAR_5_8.equals(model))
106 || (TelescopeModel.CELESTRON_NEXSTAR_GPS.equals(model))
107 || (TelescopeModel.CELESTRON_NEXSTAR_GT.equals(model))
108 || (TelescopeModel.CELESTRON_NEXSTAR_OGT.equals(model))) {
109 name = CELESTRON_CLASS_NAME;
110
111 } else if ((TelescopeModel.MEADE.equals(model))
112 || (TelescopeModel.MEADE_LX200.equals(model))
113 || (TelescopeModel.MEADE_LX200.equals(model))
114 || (TelescopeModel.MEADE_LX200_16.equals(model))
115 || (TelescopeModel.MEADE_AUTOSTAR.equals(model))) {
116 name = MEADE_CLASS_NAME;
117 } else {
118 throw new IllegalArgumentException("TelescopeModel not available");
119 }
120
121 try {
122 instance = (TelescopeInterface) Class.forName(name).newInstance();
123 instance.setTelescopeModel(model);
124 } catch (ClassNotFoundException e) {
125 throw new IllegalArgumentException(
126 "Unable to instantiate " + name);
127
128 } catch (InstantiationException e) {
129 throw new IllegalArgumentException(
130 "Unable to instantiate " + name);
131 } catch (IllegalAccessException e) {
132 throw new IllegalArgumentException(
133 "Unable to instantiate " + name); }
134
135 return instance;
136
137 }
138
139 /*** Load properties file if it exists
140 * @return Properties Telescope propertis
141 */
142 public static Properties loadProperties() {
143 Properties telescopeProperties = new Properties();
144 String homeDir = System.getProperties().getProperty("user.home");
145 String propFile = homeDir + File.separator + "telescope.properties";
146 File fil = new File(propFile);
147
148 try {
149 if (fil.exists()) {
150 telescopeProperties.load(new FileInputStream(propFile));
151 System.out.println("loaded Telescope property file from "+propFile);
152 }
153 } catch (IOException e) {
154 System.out.println("Can't load Telescope property file from "+propFile);
155 System.out.println(e);
156 }
157
158 return telescopeProperties;
159 }
160
161 }
162
163
This page was automatically generated by Maven