FreeWRL / FreeX3D 4.3.0
BrowserFactory.java
1package sai;
2import org.web3d.x3d.sai.*;
3import java.util.*;
4import java.io.*;
5import java.applet.*;
6import java.net.*;
7
8public class BrowserFactory {
9
10 private static BrowserFactoryImpl freewrlFactory;
11 private static Properties freewrlProperties;
12
13 static {
14
15 freewrlProperties = new Properties();
16
17 InputStream is = BrowserFactory.class.getClassLoader().getResourceAsStream("freewrlsai.properties");
18
19 if (is == null) {
20 freewrlProperties.put("factory", "sai.FreeWRLFactory");
21 } else {
22 try {
23 freewrlProperties.load(is);
24 } catch (IOException e) {
25 System.out.println(e);
26 }
27 }
28 }
29
30
31 private BrowserFactory() {
32
33 }
34
35 public static void setBrowserFactoryImpl(BrowserFactoryImpl fac) throws IllegalArgumentException, X3DException, SecurityException {
36 if (freewrlFactory != null) {
37 throw new X3DException("Factory has already been defined");
38 }
39
40 if (fac == null) {
41 throw new IllegalArgumentException("Null factory passed to setBrowserFactoryImpl");
42 }
43
44 freewrlFactory = fac;
45 }
46
47 public static X3DComponent createX3DComponent(Map params) throws NotSupportedException {
48 if (freewrlFactory == null)
49 loadFactory();
50
51 return freewrlFactory.createX3DComponent(params);
52 }
53
54 public static ExternalBrowser getBrowser(Applet applet) throws NotSupportedException, NoSuchBrowserException {
55 if (freewrlFactory == null)
56 loadFactory();
57
58 ExternalBrowser b = freewrlFactory.getBrowser(applet);
59
60 if (b == null) {
61 throw new NoSuchBrowserException("getBrowser(Applet): no such browser found");
62 }
63
64 return b;
65 }
66
67 public static ExternalBrowser getBrowser(Applet applet, String frameName, int index) throws NotSupportedException, NoSuchBrowserException {
68 if (freewrlFactory == null)
69 loadFactory();
70
71 ExternalBrowser b = freewrlFactory.getBrowser(applet);
72
73 if (b == null) {
74 throw new NoSuchBrowserException("getBrowser(Applet, String, int): no such browser found");
75 }
76
77 return b;
78
79 }
80
81 public static ExternalBrowser getBrowser(InetAddress address, int port) throws NotSupportedException, NoSuchBrowserException, UnknownHostException, ConnectionException {
82 if (freewrlFactory == null)
83 loadFactory();
84
85 return freewrlFactory.getBrowser(address, port);
86 }
87
88 private static void loadFactory() {
89 try {
90 String factoryClassName = (String) freewrlProperties.getProperty("factory");
91 Class factoryClass = Class.forName(factoryClassName);
92 freewrlFactory = (BrowserFactoryImpl)factoryClass.newInstance();
93 } catch (Exception e) {
94 System.out.println(e);
95 }
96 }
97}