Re: [Simpleweb-Support] Multiple File Roots
Brought to you by:
niallg
|
From: Brian D. <wic...@gm...> - 2005-11-28 21:21:33
|
Hmm.. this works... can it be better?
public class Server
{
public Server(Host host) throws Exception
{
_host =3D host;
MyLoaderEngine engine =3D new MyLoaderEngine();
Iterator it =3D _host.roots();
while (it.hasNext())
{
File root =3D (File) it.next();
engine.addContext(new FileContext(root));
}
engine.load("directory",
"com.wickedfastsolutions.server.app.DirectoryListingService");
engine.load("default",
"com.wickedfastsolutions.server.app.WickedfastService");
engine.link("*", "default");
ProtocolHandler handler =3D ProtocolHandlerFactory.getInstance(engi=
ne);
handler =3D new ServerHandler(handler);
Connection connection =3D ConnectionFactory.getConnection(handler);
connection.connect(new ServerSocket(_host.getPort(), 50,
InetAddress.getByName(_host.getAddress())));
System.err.println("Current Time is " + new Date());
System.err.println("Server is running at " +
_host.getAddress() + " port " + _host.getPort());
}
public static void main(String[] args) throws Exception
{
Host host =3D new Host();
host.addRoot(new File("test/templates"));
host.addRoot(new File("test/templates2"));
host.setAddress("127.0.0.1");
host.setPort(2000);
new Server(host);
}
public Host getHost()
{
return _host;
}
private Host _host;
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
public class MyLoaderEngine implements LoaderManager, ResourceEngine
{
public MyLoaderEngine()
{
}
public void addContext(Context context)
{
try
{
_contexts.add(new LoaderEngine(context));
}
catch (IOException ex)
{
throw new RuntimeException(ex.getMessage());
}
}
public void update(String name, Loader loader)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.update(name, loader);
}
}
public void remove(String name)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.remove(name);
}
}
public void load(String name, String className) throws LoadingException
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.load(name, className);
}
}
public void load(String name, String className, Object data) throws
LoadingException
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.load(name, className, data);
}
}
public void unload(String name)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.unload(name);
}
}
public void link(String pattern, String name)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.link(pattern, name);
}
}
public void link(String pattern, String name, int pos)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.link(pattern, name, pos);
}
}
public void unlink(String pattern)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.unlink(pattern);
}
}
public void unlink(Match match)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
LoaderEngine engine =3D (LoaderEngine) it.next();
engine.unlink(match);
}
}
public synchronized Resource resolve(String target)
{
Iterator it =3D _contexts.iterator();
while (it.hasNext())
{
try
{
LoaderEngine engine =3D (LoaderEngine) it.next();
String path =3D engine.context.getRequestPath(target);
String name =3D engine.resolver.resolve(path);
if (engine.registry.contains(name))
{
Context context =3D engine.context;
File file =3D context.getLocator().getFile(target);
if (!file.isHidden() && file.canRead())
{
return engine.registry.retrieve(name);
}
}
}
catch (LocateException ex)
{
}
}
return new NotFound(new FileContext(new File("/")));
}
private class NotFound extends BasicResource {
public NotFound(Context context) {
this.context =3D context;
}
public void process(Request req, Response resp) {
handle(req, resp, new StatusReport(404));
}
}
private ArrayList _contexts =3D new ArrayList();
}
Thanks!
On 11/28/05, Niall Gallagher <gal...@ya...> wrote:
> Hi Brian,
>
> No, you do not have to rewrite the LoaderEngine to do
> this. What you can do is extend it in much the same
> way as the MapperEngine does. If you wish to add
> multiple paths to the classpath used by the loader
> engine then you can delegate to the following
> constructor.
>
> LoaderEngine(Context context, URL[] codebase);
>
> Simply add additional URLs in the form
> file:///a/b/c/path, etc. If you would like the Context
> to find files from multiple roots, then you should
> consider using the Locator. The locator performs a
> search for files from multiple locations. I typically
> use this for loading configuration files. The
> simple.http.serve.FileLocator is good for this. For
> example you could use:
>
> Locator loc =3D context.getLocator();
>
> loc.getFile("/my/file.txt");
>
> The file is searched for within all roots specified to
> the FileLocator. To increase the number of roots you
> can do the following:
>
> new FileLocator(new File[]{new File("root1"), new
> File("root2")});
>
> Also you will have to extend the FileContext like
> follows:
>
> class MyFileContext extends FileContext {
>
> private Locator loc;
>
> public MyFileContext(File base, Locator loc) {
> super(base);
> this.loc =3D loc;
> }
>
> public Locator getLocator() {
> return loc;
> }
> }
>
> Hope this helps.
>
> Niall
>
>
> --- Brian Davis <wic...@gm...> wrote:
>
> > Hi
> >
> > I am using a FileContext with a LoaderEngine to set
> > the 'root' of the
> > web server.
> >
> > FileContext context =3D new FileContext(new
> > File("webserverroot/"));
> >
> > LoaderEngine engine =3D new
> > LoaderEngine(context);
> >
> >
> > This works just dandy.. but I would like to support
> > the concept of
> > multiple server roots...
> >
> > Do I have to rewrite the LoaderEngine to accomplish
> > this?
> >
> > LoaderEngine engine =3D new LoaderEngine();
> >
> > engine.addContext(new FileContext(new
> > File("root1"));
> > engine.addContext(new FileContext(new
> > File("root2"));
> >
> > Also.. would be nice if the resolver tried to
> > resolve in root1.. then
> > root2.. then fail..
> >
> > Any help would be much appreciated..
> >
> > Thanks!
> >
> > Brian
> >
> >
> >
> -------------------------------------------------------
> > This SF.net email is sponsored by: Splunk Inc. Do
> > you grep through log files
> > for problems? Stop! Download the new AJAX search
> > engine that makes
> > searching your log files as easy as surfing the
> > web. DOWNLOAD SPLUNK!
> > http://ads.osdn.com/?ad_idv37&alloc_id=16865&op=3Dclick
> > _______________________________________________
> > Simpleweb-Support mailing list
> > Sim...@li...
> >
> https://lists.sourceforge.net/lists/listinfo/simpleweb-support
> >
>
>
> Niall Gallagher
>
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log fi=
les
> for problems? Stop! Download the new AJAX search engine that makes
> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=3D7637&alloc_id=3D16865&op=3Dclick
> _______________________________________________
> Simpleweb-Support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simpleweb-support
>
|