ServiceFactory not loading StorageFs

Hello,

I was making a sort of hello world example. The task is to read a file saved as a resource in the project and save into an external storage by using StorageFs service (just to get a hang of how the service stores the file in the external storage). The following is the code:

public class Main {
	private static final Logger LOGGER = Logger.getLogger("Main");
	public static void main(String[] args) {
		LOGGER.info("Entered Main");

		try {
			StorageFs storage = ServiceFactory.getRequiredService(StorageFs.class);
			if (storage == null) {
				throw new IllegalStateException("Storage service unavailable");
			}
			LOGGER.info("Storage not null");
			try (InputStream in = Main.class.getResourceAsStream("/data.csv");
					OutputStream outputStream = storage.store("meter_data")) {
				if (in == null) {
					// resource missing in the JAR
					throw new IllegalStateException("CSV not found in resources");
				}
				byte[] buf = new byte[8192];
				int n = 0;
				while ((n = in.read(buf)) >= 0) {
					outputStream.write(buf, 0, n);
				}
				outputStream.flush();
				System.out.println("File written successfully");

			} catch (IOException e) {
				// handle or log
				LOGGER.log(Level.SEVERE, "File write ERROR", e); //$NON-NLS-1$
			}
		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, "ServiceFactory load error:"+ e.getMessage(), e);
		}
	}

}

I am getting MissingServiceException error while running it. Here is the stack trace:

main SEVERE: ServiceFactory load error:ej.storage.fs.StorageFs
ej.service.MissingServiceException: ej.storage.fs.StorageFs
	at java.lang.Throwable.fillInStackTrace(Throwable.java:109)
	at java.lang.Throwable.<init>(Throwable.java:43)
	at java.lang.Exception.<init>(Exception.java:18)
	at java.lang.RuntimeException.<init>(RuntimeException.java:18)
	at ej.service.MissingServiceException.<init>(MissingServiceException.java:26)
	at ej.service.ServiceFactory.getRequiredService(ServiceFactory.java:213)
	at com.mycompany.newhelloworld.Main.main(Main.java:26)
	at java.lang.MainThread.run(Thread.java:915)
	at java.lang.Thread.runWrapper(Thread.java:388)

Here is the build.gradle.kts:

plugins {
    id("com.microej.gradle.application") version "1.3.1"
}

group="com.mycompany"
version="0.1.0-RC+"

microej {
    applicationEntryPoint = "com.mycompany.newhelloworld.Main"
}

dependencies {
    implementation("ej.api:edc:1.3.7")
	implementation("ej.library.runtime:storage-fs:1.2.0")
	implementation("ej.library.runtime:storage:1.2.0")
	implementation("ej.library.runtime:service:1.2.0")
	implementation("ej.library.eclasspath:logging:1.2.1")
    implementation("ej.api:fs:2.1.1")
    implementation("ej.library.runtime:property:4.2.0")
    microejVee("com.nxp.vee.mimxrt1170:vee-port:3.1.0")
}

I have added the following line in configuration>common.properties:

# Storage root directory
ej.storage.fs.StorageFs.root=microej

I have uncommented this line in configuration.properties file for the VEE Port Project:

# Enable the External Resources module (disabled by default).
com.microej.runtime.externalresourceloader.enabled=true

I thought, since the StorageFs was a build in implementation of Storage, I don’t need to specify the implementation in any properties file.

I needed help in understanding what I might be doing wrong (is it even feasible?).

Thanks

Hello Siji,

Thanks for the detailed report! I wasn’t able to reproduce the MissingServiceException yet, but we’re investigating on our side.
In the meantime, please check that StorageFs is correctly registered in the ServiceFactory : Kernel and Features Communication — MicroEJ Documentation
Let us know if registering the service solves your issue, and we’ll keep you updated as soon as we finish reproducing the exact error on our side.

Best regards,

Hanae from MicroEJ

The error was produced when I built the application as mono-sandbox application (I guess in SDK6, there is no usage of Standalone Application as a term) with the VEE Port project build included.

I checked that Storage works for multi-sandbox application with the kernel-GREEN project Ver. No 2.1.1

Was able to resolve the issue (MissingServiceException) by adding these files (with the specified content):

  • src>main>resources>hello-world.properties.list
# Implementation class of Storage Service
ej.storage.Storage=ej.storage.fs.StorageFs

# Storage root directory
ej.storage.fs.StorageFs.root=microej
  • src>main>resources>hello-world.types.list
ej.storage.Storage
ej.storage.fs.StorageFs

Also changed from

StorageFs storage = ServiceFactory.getRequiredService(StorageFs.class);

to

Storage storage = ServiceFactory.getRequiredService(Storage.class);

Thanks,

Siji Sukumaran

Glad to hear you were able to resolve the issue.

Thank you for sharing the details of your fix.

Hanae from MicroEJ