Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
I still wonder
where your code would be physically when its used
and what would make the JVM execute your code.
How would the client code use your code?
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
Quote:
Originally Posted by
Norm
I still wonder
where your code would be physically when its used
and what would make the JVM execute your code.
How would the client code use your code?
OMG, instead of getting my questions answered I end up explaining stuff to people who are supposed to give me answers. Funny way to run a business.
But, here we go. My classes are loaded by the JVM from an SQL database and across the network, as I hinted above: "Think of it more like a set of distributed sources of data, including database(s) and network(s)." They come through as single entities, not packed in jars. Even if they were (in jars), I'd still end up with the same problem: how to tell the JVM to extract service data from META-INF/services. I'd expect that such a task would be done automatically (from a jar on a physical file system) merely as a convenience and that there would still be a chance to do it manually/programmatically. But that information is somehow very hard to come by. Nobody seems to know if it can be done at all, let alone how.
The client wouldn't use my code directly. It would use a FileSystem provided through FileSystems.getFileSystem(URI), as I've explained above on numerous occasions.
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
I've written hundreds of programs in java but have not covered all of its facilities. Not many people know it all.
The volunteers that work forums are probably not as knowledgeable as a professional that works a full shift.
You have a very interesting and very specialized problem.
Have you asked this question on: http://stackoverflow.com/questions/tagged/java
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
Quote:
Originally Posted by
Norm
I've written hundreds of programs in java but have not covered all of its facilities. Not many people know it all.
The volunteers that work forums are probably not as knowledgeable as a professional that works a full shift.
You have a very interesting and very specialized problem.
Have you asked this question on:
http://stackoverflow.com/questions/tagged/java
Yes. Yes, I have. Here it is: http://stackoverflow.com/questions/3...a-inf-services
Suprisingly, no answers there. Perhaps I should nag Larry directly. :)
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
I played with URLStreamHandlerFactory several years ago. It allows you to define your own protocol.
Code:
URL.setURLStreamHandlerFactory(new TestFactory());
try {
URL u = new URL("xttp://localhost:8080");
Object content = u.getContent();
There were several more classes involved, but nothing complicated.
There is probably a security concern that prevented Oracle from putting in a runtime interface for FileSystems.
============================================
I just noticed something when executing my test programs for your problem. I have built a META-INF folder with a services folder etc in the folder with my test program. I had put into a jar file for earlier testing and it worked, as expected. Now I tried executing my test program from a class file with the -cp . option. It worked -> the JVM is reading my META-INF folder and finding my FileSystemProvider. Ok. What if I rename the folder: META-INFXX.
The FS provider was not found. OK. Now what if my program renames the META-INFXX folder to META-INF before calling the ServiceLoader. It worked.
My test code
Code:
public class Norms_Test {
public static void main(String[] args) throws Exception {
File metaF = new File("META-INFXX");
System.out.println(metaF +" exists="+metaF.exists());
metaF.renameTo(new File("META-INF")); // Will this be found ???
ServiceLoader<FileSystemProvider> sl = ServiceLoader.load(FileSystemProvider.class, ClassLoader.getSystemClassLoader());
// ServiceConfigurationError may be throw here
for (FileSystemProvider provider: sl) {
System.out.println("0scheme="+ provider.getScheme()); // scheme=jar
}
java.util.List<FileSystemProvider> listfsp = FileSystemProvider.installedProviders();
System.out.println(listfsp); // [sun.nio.fs.WindowsFileSystemProvider@12204a1, com.sun.nio.zipfs.ZipFileSystemProvider@a298b7]
for(int i=0; i < listfsp.size(); i++) {
System.out.println("1scheme="+listfsp.get(i).getScheme()); // file jar
}
/* Define File System Properies in HashMap */
Map<String, String> properties = new HashMap<>();
/* set create to true if you want to create a new ZIP file */
properties.put("create", "true");
/* specify encoding to UTF-8 */
properties.put("encoding", "UTF-8");
/* Locate File on disk for creation */
URI uri = URI.create("norms://aFile");
System.out.println("uri="+uri); // uri=norms:///
// http://jakubstas.com/file-system-api/#.Vs4t55xum00
// To install a file system provider just place a jar file in your applications class path
// this jar contains configuration file called java.nio.file.spi.FileSystemProvider in the resource directory
// META-INF/services containing fully qualified names of one or more file system providers.
// provider classes must have a zero-argument constructor so that they can be instantiated during loading.
// A service provider is identified by placing a provider-configuration file in the resource directory META-INF/services.
// The file's name is the fully-qualified binary name of the service's type.
// The file contains a list of fully-qualified binary names of concrete provider classes, one per line.
/* Create file System */
try (FileSystem fs = FileSystems.newFileSystem(uri, properties)) {
System.out.println("fs="+fs); //<<<<<<<
}
}
}
I didn't clean it up. It has stuff from several tests.
The console from when I executed:
Code:
Running: java.exe -client -cp . Norms_Test
META-INFXX exists=true
0scheme=jar
MyFSP empty constr
getScheme() called
0scheme=norms
MyFSP empty constr
getScheme() called
[sun.nio.fs.WindowsFileSystemProvider@14991ad, com.sun.nio.zipfs.ZipFileSystemProvider@d93b30, MyFileSystemProvider@16d3586]
1scheme=file
1scheme=jar
getScheme() called
1scheme=norms
uri=norms://aFile
getScheme() called
nFS uri=norms://aFile, env={create=true, encoding=UTF-8}
Exception in thread "main" java.lang.NullPointerException
at MyFileSystemProvider.newFileSystem(MyFileSystemProvider.java:63)
at java.nio.file.FileSystems.newFileSystem(Unknown Source)
at java.nio.file.FileSystems.newFileSystem(Unknown Source)
at Norms_Test.main(Norms_Test.java:70)
0 error(s)
The NPE is because I don't have a FileSystem defined.
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
Re: Implementing FileSystem without MANIFEST.MF/META-INF/services
Oh, and if someone wonders... I haven't opted for Norms workaround either. I think it's not something a professional solution can do. Instead, I made my own implementation of FileSystem available via JMX MBean. The client can still obtain it with a one-liner, albeit somewhat longer than the unrealized ideal FileSystems.getFileSystem(URI).