• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

custom class loader for jars stored in blobs  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a need to store jar files in the database and retrieve them and load the classes in them at runtime.

I use JPA/Hibernate and have an entity with a byte[] column. I want to avoid writing to the filesystem, but JarFile doesn't seem to have the ability to read from a stream only from a file.

Does anyone have an example of a JarFile being stored in the database with ORM or directly in a blobs table?

MO
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mike!

You seem to have no problems with reading data from the blob, so I'm going to move your question to a Java in General forum.
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easy to turn a byte array extracted from your database to an InputStream using ByteArraysInputStream though I would use a Blob as the column type because JDBC defines methods that return a Blob as in InputStream. The InputStream can then be used to generate a JarInputStream and from that the jar entries can be extracted. One would then have to write an implementation of ClassLoader probably using a Map to hold the jar entries keyed by their class names and/or resource names. Though non-trivial this last stage is not difficult.

I would be surprised if there is not a 'commons' library that does most of this for you. I will let you do the research.
 
Mike Oliver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Tookey wrote:It is easy to turn a byte array extracted from your database to an InputStream using ByteArraysInputStream though I would use a Blob as the column type because JDBC defines methods that return a Blob as in InputStream. The InputStream can then be used to generate a JarInputStream and from that the jar entries can be extracted. One would then have to write an implementation of ClassLoader probably using a Map to hold the jar entries keyed by their class names and/or resource names. Though non-trivial this last stage is not difficult.

I would be surprised if there is not a 'commons' library that does most of this for you. I will let you do the research.



Well I decided to use Hibernate and the entity has the meta data about the jar and a byte[] for the jar content. This works well with Spring JSF. So I fetch the entity and read the byte[] into a JarInputStream and get the manifest and load the classes from there. So problem solved already, but I do appreciate the response.

Ollie
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Tookey wrote:One would then have to write an implementation of ClassLoader probably using a Map to hold the jar entries keyed by their class names and/or resource names. Though non-trivial this last stage is not difficult.



There's a method that loads a class from a byte array in my File Class Loader.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!