Hello,

I'm attempting to create a service to be deployed in karaf that accepts a
file and imports it into a memory repository. Progress-wise, my command is
currently deployed on karaf and I can execute it, however my import service
is consistently null.

My work is set up as follows: I have a project org-myproject-cli to handle
the command line interface, and another project org-myproject-etl to handle
the actual service implementation. In the cli project my main class is
CLIImporter, while in the etl project I have an interface and
implementation classes for the Import Service. You'll find these files
attached to this email. (Note: the implementations are unfinished, I'm
caught up with the service being null.)

I'm utilizing a blueprint.xml file to set up the command:




















*<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0
<http://www.osgi.org/xmlns/blueprint/v1.0.0>">    <command-bundle
xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0
<http://karaf.apache.org/xmlns/shell/v1.0.0>">        <command
name="myproject/import">            <action
class="org.myproject.cli.importer.CLIImporter" />        </command>
</command-bundle>    <bean id="cliImportService"
class="org.myproject.cli.importer.CLIImporter">        <property
name="importService" ref="importServiceBean"/>    </bean>    <reference
id="importServiceBean"
interface="org.myproject.etl.rdf.importer.api.RDFImportService"
availability="mandatory">    </reference></blueprint>*

which seems to be set up fine, and I can execute the command, but the
service itself is always null:





*karaf@root()> myproject:import repId /home/zachary/Downloads/mockData.ttl
truerepId /home/zachary/Downloads/mockData.ttl true nullError executing
command: java.lang.NullPointerException*

As you see in the CLIImporter's doExecute(), the middle line has the
parameters being printed, with the import service null. I'm using
declarative services to set up the repository.

Here are the headers I get on karaf from the cli and service
implementations (in that order):
























*importer (294)--------------Bnd-LastModified = 1442953920339Build-Jdk =
1.7.0_85Built-By = zacharyCreated-By = Apache Maven Bundle
PluginManifest-Version = 1.0Tool = Bnd-1.50.0Bundle-ManifestVersion =
2Bundle-Name = importerBundle-SymbolicName = importerBundle-Version =
0.0.1.SNAPSHOTImport-Service =
org.myproject.etl.rdf.importer.api.RDFImportService;multiple:=falseImport-Package
=     org.apache.felix.gogo.commands;version="[0.10,1)",
org.apache.karaf.shell.console;version="[2.2,3)",
org.myproject.etl.rdf.importer.api;version="[0.0,1)",
org.openrdf.repository;version="[2.7,3)",
org.openrdf.rio;version="[2.7,3)",
org.osgi.service.blueprint;version="[1.0.0,2.0.0)"*






























*rdf.import (291)----------------Bnd-LastModified = 1442862863229Build-Jdk
= 1.7.0_85Built-By = zacharyCreated-By = Apache Maven Bundle
PluginManifest-Version = 1.0Service-Component =
OSGI-INF/org.myproject.etl.rdf.importer.impl.RDFImportServiceImpl.xmlTool =
Bnd-1.50.0Bundle-ManifestVersion = 2Bundle-Name =
rdf.importBundle-SymbolicName = rdf.importBundle-Version =
0.0.1.SNAPSHOTImport-Service =
org.openrdf.repository.RepositoryExport-Package =
org.myproject.etl.rdf.importer.api;        uses:=org.openrdf.rio;
version=0.0.1.SNAPSHOTImport-Package =
org.myproject.etl.rdf.importer.api,
org.openrdf.model;version="[2.7,3)",
org.openrdf.model.impl;version="[2.7,3)",
org.openrdf.repository;version="[2.7,3)",
org.openrdf.rio;version="[2.7,3)",
org.osgi.service.component;version="[1.1,2)"*

Also attached are the pom files for the respective projects. I've been
struggling with this issue for a little while now, so if someone could
provide some insight onto why the service is remaining null, I'd very much
appreciate it!

Thanks,

-Zach
package org.myproject.cli.importer;

import java.io.File;

import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.openrdf.rio.RDFFormat;
import org.myproject.etl.rdf.importer.api.RDFImportService;



@Command(scope = "myproject", name = "import", description = "Imports objects to a repository")
public class CLIImporter extends OsgiCommandSupport{

	
	private RDFImportService importService;
	

	@Argument(index = 0, name = "repId", description = "The id of the repository the file will be imported to", required = true, multiValued = false)
	String repositoryId = null;

	@Argument(index = 1, name = "file", description = "The file to be imported into the repository", required = true, multiValued = false)
	String file = null;

	@Argument(index = 2, name = "continueOnError", description = "Optional: If true, continue parsing even if there is an error on a line.", required = true, multiValued = false)
	boolean continueOnError = false;

	@Argument(index = 3, name = "fileType", description = "Optional: Specify the file type if the one given is unsupported by Sesame.", required = false, multiValued = false)
	String fileType = null;

	@Argument(index = 4, name = "help", description = "Optional: If true, display a help menu.", required = false, multiValued = false)
	boolean help = false;

	

	public RDFImportService getImportService() {
		return importService;
	}

	
	public void setImportService(RDFImportService importService) {
		this.importService = importService;
	}

	@Override
	protected Object doExecute() throws Exception {
		
		File newFile = new File(file);
		
		System.out.println(repositoryId + " " + file + " " + continueOnError + " " + importService);
		
		if(fileType == null && help == false){
			importService.importFile(repositoryId, newFile, continueOnError);
		}
		else if(help == false){
			importService.importFile(repositoryId, newFile, continueOnError, RDFFormat.forMIMEType(fileType));
		}
		else if(fileType == null){
		//	impService.importFile(repositoryId, file, continueOnError, help);
		}
		else{
		//	impService.importFile(repositoryId, file, continueOnError, fileType, help);
		}
		
		return null;
	}
}
package org.myproject.etl.rdf.importer.api;

import java.io.File;
import org.openrdf.rio.RDFFormat;

/**
 * Created by bryan on 9/10/15.
 */
public interface RDFImportService {

    /**
     * Imports a triple file to a specified repository.
     * @param repositoryID The id of the repository to import triples to
     * @param file The file to import triples from
     * @param cont An option to continue import with next triple if error occurs. Warnings will be given.
     */
    public void importFile(String repositoryID,  File file, Boolean cont);

    /**
     * Imports a triple file to a specified repository.
     * @param repositoryID The id of the repository to import triples to
     * @param file The file to import triples from
     * @param cont An option to continue import with next triple if error occurs. Warnings will be given.
     * @param format The file format for the imported file
     */
    
    public void importFile(String repositoryID,  File file, Boolean cont, RDFFormat format);


}
package org.myproject.etl.rdf.importer.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.myproject.etl.rdf.importer.api.RDFImportService;
import org.openrdf.model.Resource;
import org.openrdf.model.impl.BNodeImpl;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.Rio;
import org.osgi.service.component.ComponentContext;

import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Reference;

@Component(provide = RDFImportService.class, immediate=true)
public class RDFImportServiceImpl implements RDFImportService{
	
	
	
	private Repository repository;
	
	private ComponentContext context;
	
	private RepositoryConnection repConnect;
	
	private Resource resource = new BNodeImpl("string");
	

	@Reference(target="(repositorytype=memory)")
	public void setRepository(Repository repository) {
		this.repository = repository;
	}
	
	
	
	
	public void importFile(String repositoryID,  File file, Boolean cont) {
		
		try {
			if (file == null) {
				throw new FileNotFoundException();
			}

			RDFFormat format = Rio.getParserFormatForFileName(file.getName());
		

			if (format == null) {
				throw new IOException();
			}

			System.out.println("Repository connecting...");
			repConnect = repository.getConnection();
			
			repConnect.add(file, null, format, resource);

			System.out.println("Repository set!");
			System.out.println(repConnect.isEmpty());

			System.out.println("Executing Import command");
			System.out.println("Arguments passed: " + repositoryID + " " + file);
			
			
			
			

		} catch (FileNotFoundException e) {
			System.out.println("FnF Exception!");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException! File type not supported by Sesame.");
			e.printStackTrace();
		} catch (RepositoryException e) {
			System.out.println("Repository Exception!");
			e.printStackTrace();
		} catch (RDFParseException e) {
			System.out.println("RDFParseException! There was an error parsing the RDF file.");
			e.printStackTrace();
		}

	}

	
	public void importFile(String repositoryID,  File file, Boolean cont, RDFFormat format)  {
		
		try {
			if (file == null) {
				throw new FileNotFoundException();
			}
			
			if (format == null) {
				format = Rio.getParserFormatForFileName(file.getName());
				
				if(format == null){
					throw new IOException();
				}
			}

			repConnect = repository.getConnection();
			
			String newURI = "com.inovexcorp.myproject." + repositoryID;
			
			repConnect.add(file, newURI, format);
			

			System.out.println("Repository set!");
			System.out.println(repConnect.isEmpty());

			System.out.println("Executing Import command");
			System.out.println("Arguments passed: " + repositoryID + " " + file);
			
			
			
			
			repConnect.close();
			

		} catch (FileNotFoundException e) {
			System.out.println("FnF Exception!");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException! File type not supported by Sesame.");
			e.printStackTrace();
		} catch (RepositoryException e) {
			System.out.println("Repository Exception!");
			e.printStackTrace();
		} catch (RDFParseException e) {
			System.out.println("RDFParseException! There was an error parsing the RDF file.");
			e.printStackTrace();
		} 
		

	}
	
	
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0";
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd";>
    <parent>
        <artifactId>parent</artifactId>
        <groupId>org.myproject.cli</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>importer</artifactId>
	<packaging>bundle</packaging>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.karaf.shell</groupId>
			<artifactId>org.apache.karaf.shell.console</artifactId>
			<version>2.2.11</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>18.0</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-runtime-osgi</artifactId>
			<version>2.7.14</version>
		</dependency>
		<dependency>
			<groupId>biz.aQute.bnd</groupId>
			<artifactId>bndlib</artifactId>
			<version>2.3.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-collections4</artifactId>
			<version>4.0</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-rio-rdfxml</artifactId>
			<version>2.7.14</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-rio-turtle</artifactId>
			<version>2.7.14</version>
		</dependency>

		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>javax.ws.rs-api</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>com.beust</groupId>
			<artifactId>jcommander</artifactId>
			<version>1.48</version>
		</dependency>

		<dependency>
			<groupId>org.myproject.etl</groupId>
			<artifactId>rdf.import</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<type>bundle</type>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.3.7</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Import-Package>*</Import-Package>
						<Export-Package></Export-Package>
						<Service-Component>*</Service-Component>
					</instructions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
			</plugin>


		</plugins>
	</build>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd";>
	<parent>
		<artifactId>parent</artifactId>
		<groupId>org.myproject.etl</groupId>
		<version>0.0.1-SNAPSHOT</version>

	</parent>
	<modelVersion>4.0.0</modelVersion>
	<packaging>bundle</packaging>

	<artifactId>rdf.import</artifactId>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.karaf.shell</groupId>
			<artifactId>org.apache.karaf.shell.console</artifactId>
			<version>2.2.11</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>18.0</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-runtime-osgi</artifactId>
			<version>2.7.14</version>
		</dependency>
		<dependency>
			<groupId>biz.aQute.bnd</groupId>
			<artifactId>bndlib</artifactId>
			<version>2.3.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-collections4</artifactId>
			<version>4.0</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-rio-rdfxml</artifactId>
			<version>2.7.14</version>
		</dependency>
		<dependency>
			<groupId>org.openrdf.sesame</groupId>
			<artifactId>sesame-rio-turtle</artifactId>
			<version>2.7.14</version>
		</dependency>

		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>javax.ws.rs-api</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>com.beust</groupId>
			<artifactId>jcommander</artifactId>
			<version>1.48</version>
		</dependency>


	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.3.7</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Import-Package>*</Import-Package>
						<Export-Package>org.myproject.etl.rdf.importer.api</Export-Package>
						<Import-Service>org.openrdf.repository.Repository</Import-Service>
						<Service-Component>*</Service-Component>

					</instructions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.parent.build.directory}/index</outputDirectory>
							<excludeScope>provided</excludeScope>
						</configuration>
					</execution>
					<execution>
						<id>copy</id>
						<phase>package</phase>
						<goals>
							<goal>copy</goal>
						</goals>
						<configuration>
							<artifactItems>
								<artifactItem>
									<groupId>${project.groupId}</groupId>
									<artifactId>${project.artifactId}</artifactId>
									<version>${project.version}</version>
									<type>jar</type>
									<outputDirectory>${project.parent.build.directory}/index</outputDirectory>
								</artifactItem>
							</artifactItems>
						</configuration>
					</execution>
				</executions>
			</plugin>


		</plugins>
	</build>

</project>

Reply via email to