Hi there,

Thanks for sharing this, I've added a link to it in this RFE:

"Improve support for OSGI"
http://restlet.tigris.org/issues/show_bug.cgi?id=83

Something that isn't clear is how it helps compared to using the regular CLAP 
scheme (ClassLoader Access Protocol):
http://www.restlet.org/documentation/2.0/jse/api/org/restlet/data/LocalReference.html#CLAP_CLASS

Best regards,
Jerome
--
Restlet ~ Founder and Technical Lead ~ http://www.restlet.o​rg
Noelios Technologies ~ http://www.noelios.com



-----Message d'origine-----
De : [email protected] [mailto:[email protected]] 
Envoyé : mardi 3 août 2010 12:51
À : [email protected]
Objet : BundleResource Loader for use in OSGI

Created a simple BundleResourceClientHelper for use in an OSGI environment. 
It's probably ugly, but wanted to share it anyway. 

======== Start code============
public class BundleResourceClientHelper extends LocalClientHelper {
        public static final Protocol BUNDLERESOURCE = new 
Protocol("bundleresource","BUNDLERESOURCE",
            "OSGI Bundle Resource", Protocol.UNKNOWN_PORT);
        
        public BundleResourceClientHelper(Client client)
        {
                super(client);
                
                getProtocols().add(BUNDLERESOURCE);
        }
        
        @Override
        protected void handleLocal(Request request, Response response,
                        String decodedPath)
        {
                String scheme = request.getResourceRef().getScheme();
                if(BUNDLERESOURCE.getSchemeName().equalsIgnoreCase(scheme))
                {
                        handleFile(request,response);
                }
                else
                {
                        throw new IllegalArgumentException("Protocol " + scheme 
+ " not supported, only BUNDLERESOURCE is supported");
                }
        }
        
        private void handleFile(Request request, Response response)
        {
                if(Method.GET.equals(request.getMethod()) || 
Method.HEAD.equals(request.getMethod()))
                {
                        handleGet(request,response);
                }
                else {
            response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
            response.getAllowedMethods().add(Method.GET);
            response.getAllowedMethods().add(Method.HEAD);
        }
        }
        
        private void handleGet(Request request, Response response)
        {
                try
                {
                        String path = request.getResourceRef().getPath();
                        URL url = new URL(request.getResourceRef().toString());
            Representation output = new InputRepresentation(url
                    .openStream(), getMetadataService()
                    .getDefaultMediaType());
            output.setLocationRef(request.getResourceRef());
            output.setModificationDate(new Date());

            // Update the expiration date
            long timeToLive = getTimeToLive();

            if (timeToLive == 0) {
                output.setExpirationDate(null);
            } else if (timeToLive > 0) {
                output.setExpirationDate(new Date(System
                        .currentTimeMillis()
                        + (1000L * timeToLive)));
            }

            // Update the metadata based on file extensions
            String name = path.substring(path.lastIndexOf('/') + 1);
            Entity.updateMetadata(name, output, true,
                    getMetadataService());

            // Update the response
            response.setEntity(output);
            response.setStatus(Status.SUCCESS_OK);                              
                
                }
                catch(Exception e)
                {
                        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);      
                
                }
        }
}

===== End code ===== 

Usage: 
Step 1: enable the client helper in the restlet engine:
Engine.getInstance().getRegisteredClients().add(new 
BundleResourceClientHelper(null));

Step 2: enable the protocol on your component: 
component.getClients().add(BundleResourceClientHelper.BUNDLERESOURCE);

Step 3: call a resource from your (running) bundle: 
new Directory(getContext(), new 
Reference(getClass().getClassLoader().getResource("/www")));

It's probably already done, but I could not find it anyware. 

Note: some kind of OSGI scheme notation could be used with some small 
modifications

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2642292

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2681652

Reply via email to