[P2play-commit] SF.net SVN: p2play: [11] trunk/P2Play/src/org/p2play
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-26 23:21:52
|
Revision: 11
http://p2play.svn.sourceforge.net/p2play/?rev=11&view=rev
Author: tisoft
Date: 2006-12-26 15:21:50 -0800 (Tue, 26 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Added Paths:
-----------
trunk/P2Play/src/org/p2play/resource/
trunk/P2Play/src/org/p2play/resource/Resource.java
trunk/P2Play/src/org/p2play/resource/ResourceClassLoader.java
trunk/P2Play/src/org/p2play/resource/ResourceContainer.java
trunk/P2Play/src/org/p2play/resource/ResourceListener.java
trunk/P2Play/src/org/p2play/resource/ResourceManager.java
trunk/P2Play/src/org/p2play/resource/ResourceURLConnection.java
trunk/P2Play/src/org/p2play/resource/ResourceURLStreamHandler.java
trunk/P2Play/src/org/p2play/resource/impl/
trunk/P2Play/src/org/p2play/resource/impl/ByteArrayResource.java
trunk/P2Play/src/org/p2play/resource/impl/LocalResourceManager.java
Added: trunk/P2Play/src/org/p2play/resource/Resource.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/Resource.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/Resource.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import java.io.InputStream;
+
+import rice.p2p.past.gc.GCPastContent;
+
+public interface Resource extends GCPastContent{
+ public byte[] getData();
+ public InputStream getDataAsStream();
+}
Added: trunk/P2Play/src/org/p2play/resource/ResourceClassLoader.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceClassLoader.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceClassLoader.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+
+public class ResourceClassLoader extends ClassLoader {
+ private ResourceManager resourceManager;
+
+ private static ResourceClassLoader resourceClassLoader;
+
+ public static ResourceClassLoader getResourceClassLoader() {
+ if (resourceClassLoader == null)
+ throw new IllegalStateException(
+ "ResourceClassLoader has not been created yet.");
+ return resourceClassLoader;
+ }
+
+ public static void create(ResourceManager resourceManager) {
+ if (resourceClassLoader != null)
+ throw new IllegalStateException(
+ "ResourceClassLoader has already been created.");
+ resourceClassLoader = new ResourceClassLoader(resourceManager);
+ }
+
+ private ResourceClassLoader(ResourceManager resourceManager) {
+ super();
+ this.resourceManager = resourceManager;
+ }
+
+ @Override
+ public URL getResource(String name) {
+ try {
+ return new URL("PAST", "localhost", 0, name, resourceManager
+ .getURLStreamHandler());
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ private HashSet<String> failedClasses = new HashSet<String>();
+
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ if (failedClasses.contains(name))
+ throw new ClassNotFoundException();
+ else {
+ try {
+ return super.loadClass(name);
+ } catch (ClassNotFoundException e) {
+ failedClasses.add(name);
+ throw e;
+ }
+ }
+ }
+}
Added: trunk/P2Play/src/org/p2play/resource/ResourceContainer.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceContainer.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceContainer.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import javolution.util.FastSet;
+import javolution.util.FastCollection.Record;
+import rice.Continuation;
+
+public class ResourceContainer implements Continuation {
+ private Resource resource;
+
+ private String name;
+
+ private FastSet<ResourceListener> listeners = new FastSet<ResourceListener>();
+
+ private Exception exception;
+
+ public ResourceContainer(String name) {
+ this.name = name;
+ }
+
+ ResourceContainer(String name, Resource resource) {
+ this(name);
+ this.resource = resource;
+ }
+
+ public Resource getResource() {
+ if (exception != null) {
+ throw new RuntimeException(exception);
+ }
+ return resource;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ void setResource(Resource resource) {
+ this.resource = resource;
+ this.exception = null;
+ notifyListeners();
+ }
+
+ void setException(Exception exception) {
+ this.resource = null;
+ this.exception = exception;
+ notifyListeners();
+ }
+
+ public Exception getException() {
+ return exception;
+ }
+
+ public boolean isReady() {
+ return resource != null || exception != null;
+ }
+
+ public void receiveResult(Object result) {
+ setResource((Resource) result);
+ }
+
+ public void receiveException(Exception result) {
+ setException(result);
+ result.printStackTrace();
+ }
+
+ public void removeResourceListener(ResourceListener listener) {
+ synchronized (listeners) {
+ listeners.remove(listener);
+ }
+ }
+
+ public void addResourceListener(ResourceListener listener) {
+ synchronized (listeners) {
+ listeners.add(listener);
+ }
+ }
+
+ private void notifyListeners() {
+ synchronized (listeners) {
+ for (Record r = listeners.head(), end = listeners.tail(); (r = r.getNext()) != end;) {
+ if (exception != null) {
+ listeners.valueOf(r).resourceException(this);
+ } else {
+ listeners.valueOf(r).resourceUpdated(this);
+ }
+ }
+ }
+ }
+}
Added: trunk/P2Play/src/org/p2play/resource/ResourceListener.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceListener.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceListener.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+public interface ResourceListener {
+ public void resourceUpdated(ResourceContainer container);
+ public void resourceException(ResourceContainer container);
+}
Added: trunk/P2Play/src/org/p2play/resource/ResourceManager.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceManager.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceManager.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import java.net.URLStreamHandler;
+
+
+/**
+ * Used to load static resources
+ *
+ * @author markush
+ *
+ */
+public interface ResourceManager {
+ /**
+ * Loads the Resource with the given name
+ *
+ * @param name
+ * The name of the Resource to load
+ * @return the loaded resource
+ * @throws Exception
+ * thrown if the resource could not be retrieved or is not found
+ */
+ public ResourceContainer loadResource(String name);
+
+ public ResourceContainer loadResource(String name, ResourceListener listener);
+
+ public URLStreamHandler getURLStreamHandler();
+}
Added: trunk/P2Play/src/org/p2play/resource/ResourceURLConnection.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceURLConnection.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceURLConnection.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class ResourceURLConnection extends URLConnection implements ResourceListener {
+
+ private String name;
+
+ private ResourceManager manager;
+
+ private ResourceContainer container;
+
+ public ResourceURLConnection(URL url, ResourceManager manager) {
+ super(url);
+ this.name = url.getFile();
+ this.manager = manager;
+
+ }
+
+ @Override
+ public void connect() throws IOException {
+ if (this.container == null) {
+ this.container = manager.loadResource(name, this);
+ }
+ connected = true;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (container == null)
+ connect();
+
+ InputStream inputStream;
+
+ if (!container.isReady()) {
+ synchronized (container) {
+ try {
+ container.wait();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ inputStream = this.container.getResource().getDataAsStream();
+
+ return inputStream;
+ }
+
+ public void resourceException(ResourceContainer container) {
+ synchronized (container) {
+ container.notifyAll();
+ }
+ }
+
+ public void resourceUpdated(ResourceContainer container) {
+ synchronized (container) {
+ container.notifyAll();
+ }
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/resource/ResourceURLStreamHandler.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/ResourceURLStreamHandler.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/ResourceURLStreamHandler.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+public class ResourceURLStreamHandler extends URLStreamHandler {
+ private ResourceManager resourceManager;
+
+ public ResourceURLStreamHandler(ResourceManager resourceManager) {
+ super();
+ this.resourceManager = resourceManager;
+ }
+
+ @Override
+ protected URLConnection openConnection(URL u) throws IOException {
+ return new ResourceURLConnection(u, resourceManager);
+ }
+
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/resource/impl/ByteArrayResource.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/impl/ByteArrayResource.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/impl/ByteArrayResource.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.p2play.resource.Resource;
+
+import rice.p2p.commonapi.Id;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.past.Past;
+import rice.p2p.past.PastContent;
+import rice.p2p.past.PastContentHandle;
+import rice.p2p.past.PastException;
+import rice.p2p.past.gc.GCPast;
+import rice.p2p.past.gc.GCPastContentHandle;
+import rice.p2p.past.gc.GCPastMetadata;
+
+public class ByteArrayResource implements Resource {
+ private byte[] data;
+
+ private Id id;
+
+ private long version=0;
+
+ private long expiration;
+
+ public ByteArrayResource(Id id, byte[] data, long expiration) {
+ this.data = data;
+ this.id = id;
+ this.expiration=expiration;
+ }
+
+ public ByteArrayResource(Id id, byte[] data) {
+ this(id,data,GCPast.INFINITY_EXPIRATION);
+ }
+
+ public byte[] getData() {
+ return data;
+ }
+
+ public InputStream getDataAsStream(){
+ return new ByteArrayInputStream(data);
+ }
+
+ public PastContent checkInsert(Id id, PastContent existingContent)
+ throws PastException {
+ return this;
+ }
+
+ public PastContentHandle getHandle(final Past local) {
+ return new PastContentHandle(){
+
+ public Id getId() {
+ return id;
+ }
+
+ public NodeHandle getNodeHandle() {
+ return local.getLocalNodeHandle();
+ }};
+ }
+
+ public Id getId() {
+ return id;
+ }
+
+ public boolean isMutable() {
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return id.toString()+" version: "+version+" content: "+new String(data);
+ }
+
+ public long getVersion() {
+ return version;
+ }
+
+ public long getExpiration() {
+ return expiration;
+ }
+
+ public GCPastContentHandle getHandle(final GCPast local, final long expiration) {
+ return new GCPastContentHandle(){
+
+ public Id getId() {
+ return id;
+ }
+
+ public NodeHandle getNodeHandle() {
+ return local.getLocalNodeHandle();
+ }
+
+ public long getVersion() {
+ return version;
+ }
+
+ public long getExpiration() {
+ return this.getExpiration();
+ }};
+ }
+
+ public GCPastMetadata getMetadata(long expiration) {
+ return new GCPastMetadata(this.expiration);
+ }
+
+}
Added: trunk/P2Play/src/org/p2play/resource/impl/LocalResourceManager.java
===================================================================
--- trunk/P2Play/src/org/p2play/resource/impl/LocalResourceManager.java (rev 0)
+++ trunk/P2Play/src/org/p2play/resource/impl/LocalResourceManager.java 2006-12-26 23:21:50 UTC (rev 11)
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.resource.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+import org.p2play.resource.ResourceContainer;
+import org.p2play.resource.ResourceListener;
+import org.p2play.resource.ResourceManager;
+import org.p2play.resource.ResourceURLStreamHandler;
+
+import rice.environment.Environment;
+import rice.pastry.commonapi.PastryIdFactory;
+
+public class LocalResourceManager implements ResourceManager {
+ private String baseDir;
+
+ private URLStreamHandler streamHandler;
+
+ public LocalResourceManager(String baseDir) {
+ super();
+ this.baseDir = baseDir;
+ this.streamHandler = new ResourceURLStreamHandler(this);
+ }
+
+ public ResourceContainer loadResource(final String name) {
+ final ResourceContainer rc = new ResourceContainer(name);
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ // Thread.sleep(10000);
+ URL url=getClass().getResource("/"+name);
+
+ int lenght;
+ InputStream inputStream;
+
+ if (url!=null){
+ URLConnection connection=url.openConnection();
+ lenght=connection.getContentLength();
+ inputStream=connection.getInputStream();
+ }
+ else{
+ File f = new File(baseDir + name);
+ inputStream = new FileInputStream(f);
+ lenght = (int) f.length();
+ }
+ byte[] b = new byte[lenght];
+ inputStream.read(b);
+ rc.receiveResult(new ByteArrayResource(new PastryIdFactory(
+ new Environment()).buildId(name), b));
+ } catch (Exception e) {
+ rc.receiveException(e);
+ }
+ }
+ }.start();
+ return rc;
+ }
+
+ public ResourceContainer loadResource(String name, ResourceListener listener) {
+ ResourceContainer rc = loadResource(name);
+ if (rc.isReady()) {
+ listener.resourceUpdated(rc);
+ } else
+ rc.addResourceListener(listener);
+ return rc;
+ }
+
+ public URLStreamHandler getURLStreamHandler() {
+ return streamHandler;
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|