author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 15 Nov 2008 06:53:43 +0100 | |
changeset 292 | 60bb8519cb2d |
permissions | -rw-r--r-- |
jtulach@292 | 1 |
package org.apidesign.aserverinfo.magicalbagfactory; |
jtulach@292 | 2 |
|
jtulach@292 | 3 |
import java.net.URL; |
jtulach@292 | 4 |
import org.apidesign.aserverinfo.spi.NameProvider; |
jtulach@292 | 5 |
import org.apidesign.aserverinfo.spi.ResetHandler; |
jtulach@292 | 6 |
import org.apidesign.aserverinfo.spi.ShutdownHandler; |
jtulach@292 | 7 |
import org.apidesign.aserverinfo.spi.URLProvider; |
jtulach@292 | 8 |
import org.openide.util.Lookup; |
jtulach@292 | 9 |
|
jtulach@292 | 10 |
/** A class to connect to server, identify it and manipulate with |
jtulach@292 | 11 |
* it state. The <a href="http://apidesign.org">Practical API Design</a> |
jtulach@292 | 12 |
* book used to call it AServerInfo. |
jtulach@292 | 13 |
* <p> |
jtulach@292 | 14 |
*/ |
jtulach@292 | 15 |
public final class ServerConnector { |
jtulach@292 | 16 |
public String getName() { |
jtulach@292 | 17 |
return name == null ? "noname" : name.getName(); |
jtulach@292 | 18 |
} |
jtulach@292 | 19 |
|
jtulach@292 | 20 |
public URL getURL() { |
jtulach@292 | 21 |
return url == null ? null : url.getURL(); |
jtulach@292 | 22 |
} |
jtulach@292 | 23 |
|
jtulach@292 | 24 |
public void reset() { |
jtulach@292 | 25 |
if (reset != null) { |
jtulach@292 | 26 |
reset.reset(); |
jtulach@292 | 27 |
} |
jtulach@292 | 28 |
} |
jtulach@292 | 29 |
|
jtulach@292 | 30 |
/** Additional method for API clients not available from first version. |
jtulach@292 | 31 |
* @since 2.0 |
jtulach@292 | 32 |
*/ |
jtulach@292 | 33 |
public void shutdown() { |
jtulach@292 | 34 |
if (shutdown != null) { |
jtulach@292 | 35 |
shutdown.shutdown(); |
jtulach@292 | 36 |
} |
jtulach@292 | 37 |
} |
jtulach@292 | 38 |
|
jtulach@292 | 39 |
// |
jtulach@292 | 40 |
// private part |
jtulach@292 | 41 |
// |
jtulach@292 | 42 |
|
jtulach@292 | 43 |
private final NameProvider name; |
jtulach@292 | 44 |
private final URLProvider url; |
jtulach@292 | 45 |
private final ResetHandler reset; |
jtulach@292 | 46 |
private final ShutdownHandler shutdown; |
jtulach@292 | 47 |
|
jtulach@292 | 48 |
private ServerConnector( |
jtulach@292 | 49 |
NameProvider name, URLProvider url, |
jtulach@292 | 50 |
ResetHandler reset, ShutdownHandler shutdown |
jtulach@292 | 51 |
) { |
jtulach@292 | 52 |
this.name = name; |
jtulach@292 | 53 |
this.url = url; |
jtulach@292 | 54 |
this.reset = reset; |
jtulach@292 | 55 |
this.shutdown = shutdown; |
jtulach@292 | 56 |
} |
jtulach@292 | 57 |
|
jtulach@292 | 58 |
// BEGIN: aserverinfo.magicalbag.create |
jtulach@292 | 59 |
public static ServerConnector create(final Lookup interfaces) { |
jtulach@292 | 60 |
NameProvider nameP = new NameProvider() { |
jtulach@292 | 61 |
public String getName() { |
jtulach@292 | 62 |
NameProvider p = interfaces.lookup(NameProvider.class); |
jtulach@292 | 63 |
return p == null ? "noname" : p.getName(); |
jtulach@292 | 64 |
} |
jtulach@292 | 65 |
}; |
jtulach@292 | 66 |
URLProvider urlP = new URLProvider() { |
jtulach@292 | 67 |
public URL getURL() { |
jtulach@292 | 68 |
URLProvider p = interfaces.lookup(URLProvider.class); |
jtulach@292 | 69 |
return p == null ? null : p.getURL(); |
jtulach@292 | 70 |
} |
jtulach@292 | 71 |
}; |
jtulach@292 | 72 |
ResetHandler resetP = new ResetHandler() { |
jtulach@292 | 73 |
public void reset() { |
jtulach@292 | 74 |
ResetHandler h = interfaces.lookup(ResetHandler.class); |
jtulach@292 | 75 |
if (h != null) { |
jtulach@292 | 76 |
h.reset(); |
jtulach@292 | 77 |
} |
jtulach@292 | 78 |
} |
jtulach@292 | 79 |
}; |
jtulach@292 | 80 |
// code present since version 2.0 |
jtulach@292 | 81 |
ShutdownHandler shutH = new ShutdownHandler() { |
jtulach@292 | 82 |
public void shutdown() { |
jtulach@292 | 83 |
ShutdownHandler h = interfaces.lookup(ShutdownHandler.class); |
jtulach@292 | 84 |
if (h != null) { |
jtulach@292 | 85 |
h.shutdown(); |
jtulach@292 | 86 |
} |
jtulach@292 | 87 |
} |
jtulach@292 | 88 |
}; |
jtulach@292 | 89 |
|
jtulach@292 | 90 |
return new ServerConnector(nameP, urlP, resetP, shutH); |
jtulach@292 | 91 |
} |
jtulach@292 | 92 |
// END: aserverinfo.magicalbag.create |
jtulach@292 | 93 |
} |