1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * ident "%Z%%M% %I% %E% SMI"
24 *
25 * Copyright (c) 1998-2001 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28 package com.sun.dhcpmgr.client;
29
30 import java.util.Arrays;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.text.MessageFormat;
34
35 import javax.swing.*;
36
37 import com.sun.dhcpmgr.bridge.*;
38 import com.sun.dhcpmgr.data.*;
39 import com.sun.dhcpmgr.server.*;
40
41 /**
42 * DataManager implements a central point of contact between the client and the
43 * server components; the client obtains all server object references through
44 * this class which allows us to cache information as much as possible,
45 * improving users' perception of the performance of the tool.
46 */
47 public class DataManager {
48 private DhcpMgr server;
49 private DhcpNetMgr netMgr;
50 private DhcptabMgr dhcptabMgr;
51 private DhcpServiceMgr dhcpServiceMgr;
52 private Network [] networks;
53 private DhcpClientRecord [] clients;
54 private String clientNet;
55 private Macro [] macros;
56 private Option [] options;
57 private String serverName;
58 private String shortServerName;
59 private InetAddress serverAddress;
60 private static DataManager mgr = null;
61
62 private DataManager() {
63 reset();
64 }
65
66 public synchronized void reset() {
67 // Clear the data references; used by the applet to re-initialize
68 server = new DhcpMgrImpl();
69 netMgr = null;
70 dhcptabMgr = null;
71 dhcpServiceMgr = null;
72 /*
73 * The arrays aren't nulled so that we can use them as the lock objects
74 * in the synchronized blocks below
75 */
76 networks = new Network[0];
77 clients = new DhcpClientRecord[0];
78 clientNet = null;
79 macros = new Macro[0];
80 options = new Option[0];
81 try {
82 serverAddress = InetAddress.getLocalHost();
83 setServerName(serverAddress.getHostName());
84 } catch (UnknownHostException e) {
85 serverName = shortServerName = "";
86 }
87
88 try {
89 StandardOptions.setAllOptions(
90 getDhcpServiceMgr().getInittabOptions(
91 Option.ctxts[Option.STANDARD].getCode()));
92 } catch (Throwable e) {
93 MessageFormat form = new MessageFormat(
94 ResourceStrings.getString("stdopts_init_error"));
95 Object [] args = new Object[1];
96 if (e instanceof BridgeException) {
97 args[0] = e.getMessage();
98 } else {
99 args[0] = e.toString();
100 }
101 JOptionPane.showMessageDialog(null,
102 form.format(args),
103 ResourceStrings.getString("init_error"),
104 JOptionPane.ERROR_MESSAGE);
105 }
106 }
107
108 private void setServerName(String name) {
109 serverName = name;
110 int i = serverName.indexOf('.');
111 if (i == -1) {
112 shortServerName = serverName;
113 } else {
114 shortServerName = serverName.substring(0, i);
115 }
116 }
117
118 /*
119 * Threading note: the following methods are all synchronized on the
120 * class so that server references and names are always set & retrieved
121 * in a consistent state.
122 */
123 public synchronized static DataManager get() {
124 if (mgr == null) {
125 mgr = new DataManager();
126 }
127 return mgr;
128 }
129
130 public synchronized void setServer(String name) throws Exception {
131 setServerName(name);
132 serverAddress = null;
133 }
134
135 public synchronized DhcpMgr getServer() {
136 return server;
137 }
138
139 public synchronized String getServerName() {
140 return serverName;
141 }
142
143 public synchronized String getShortServerName() {
144 return shortServerName;
145 }
146
147 public synchronized InetAddress getServerAddress() {
148 return serverAddress;
149 }
150
151 public synchronized DhcpNetMgr getDhcpNetMgr() {
152 if (netMgr == null) {
153 netMgr = getServer().getNetMgr();
154 }
155 return netMgr;
156 }
157
158 public synchronized DhcptabMgr getDhcptabMgr() {
159 if (dhcptabMgr == null) {
160 dhcptabMgr = getServer().getDhcptabMgr();
161 }
162 return dhcptabMgr;
163 }
164
165 public synchronized DhcpServiceMgr getDhcpServiceMgr() {
166 if (dhcpServiceMgr == null) {
167 dhcpServiceMgr = getServer().getDhcpServiceMgr();
168 }
169 return dhcpServiceMgr;
170 }
171
172 /*
173 * End of class-synchronized methods. Remaining methods are synchronized
174 * at a data item level.
175 */
176
177 public Network [] getNetworks(boolean forceUpdate) throws BridgeException {
178 synchronized (networks) {
179 if (forceUpdate || networks.length == 0) {
180 networks = getDhcpNetMgr().getNetworks();
181 if (networks == null) {
182 networks = new Network[0];
183 } else {
184 Arrays.sort(networks);
185 }
186 }
187 }
188 return networks;
189 }
190
191 public DhcpClientRecord [] getClients(String net, boolean forceUpdate)
192 throws BridgeException {
193 synchronized (clients) {
194 if (forceUpdate || clients.length == 0 || !net.equals(clientNet)) {
195 clients = getDhcpNetMgr().loadNetwork(net);
196 if (clients == null) {
197 clients = new DhcpClientRecord[0];
198 }
199 clientNet = net;
200 }
201 }
202 return clients;
203 }
204
205 public Macro [] getMacros(boolean forceUpdate) throws BridgeException {
206 synchronized (macros) {
207 if (forceUpdate || macros.length == 0) {
208 macros = getDhcptabMgr().getMacros();
209 if (macros == null) {
210 macros = new Macro[0];
211 }
212 }
213 }
214 return macros;
215 }
216
217 public Option [] getOptions(boolean forceUpdate) throws BridgeException {
218 synchronized (options) {
219 if (forceUpdate || options.length == 0) {
220 options = getDhcptabMgr().getOptions();
221 if (options == null) {
222 options = new Option[0];
223 }
224 /*
225 * Reload the site/vendor options portion of the global options
226 * table with the data we just loaded so that macro validation
227 * can be handled properly.
228 */
229 OptionsTable.getTable().add(options);
230 }
231 }
232 return options;
233 }
234 }