gpt4 book ai didi

java - IPOJO - 与组件类型关联的工厂无效(未启动或缺少处理程序)

转载 作者:行者123 更新时间:2023-12-01 13:36:09 26 4
gpt4 key购买 nike

我正在尝试学习如何使用 Felix iPOJO API 动态创建组件。

我有一个包含以下文件的简单 bundle :

1- HelloService.java(包含服务接口(interface))。

/*
* @author zaid almahmoud
*
*/
package helloipojo.service;

public interface HelloService
{

public void sayHello();

}

2-其实现HelloServiceImpl.java:

package helloipojo;

import helloipojo.service.HelloService;


public class HelloServiceImpl implements HelloService{

@Override
public void sayHello() {

System.out.println("Hello iPojo!");

}


}

3-Activator.java:

package helloipojo;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;


public class Activator implements BundleActivator {


public void start(BundleContext context) throws Exception {


System.out.println("Bundle Started!");


}
public void stop(BundleContext context) throws Exception {

context = null;
System.out.println("Bundle Stopped!");


}

}

4-MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloIPojo
Bundle-SymbolicName: HelloIPojo
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: helloipojo.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework

在我的应用程序中,我启动 Felix 框架并部署以下包:

iPOJO (core)
iPOJO Composite
iPOJO API

根据this来源。

接下来,我安装我的包,并实例化该组件。以下是我的类(class):

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.felix.framework.Felix;
import org.apache.felix.framework.util.FelixConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.apache.felix.ipojo.api.ComponentType;
import org.apache.felix.ipojo.api.PrimitiveComponentType;
import org.apache.felix.ipojo.api.Service;

public class HostApplication
{
private HostActivator m_activator = null;
private Felix m_felix = null;


public HostApplication()
{
// Create a configuration property map.
Map config = new HashMap();
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
// Create host activator;
m_activator = new HostActivator();
List list = new ArrayList();
list.add(m_activator);

config.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);

try
{
// Now create an instance of the framework with
// our configuration properties.
m_felix = new Felix(config);
// Now start Felix instance.
m_felix.start();
}
catch (Exception ex)
{
System.err.println("Could not create framework: " + ex);
ex.printStackTrace();
}


// Register the application's context as an OSGi service!
BundleContext bundleContext1 = m_felix.getBundleContext();


System.out.println("6");

try {

//starting ipojo required bundles
Bundle coreBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo-1.6.2.jar");
coreBundle.start();
if(coreBundle.getState()== coreBundle.ACTIVE)
System.out.println("Core Bundle is Active!");

Bundle apiBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo.api-1.6.0.jar");
apiBundle.start();
if(apiBundle.getState()== apiBundle.ACTIVE)
System.out.println("API Bundle is Active!");


Bundle compositeBundle = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Dropbox\\EBTIC\\ADERE\\feasibility-codes\\ipojo\\ipojo-distribution-1.11.0\\bundle\\org.apache.felix.ipojo.composite-1.6.0.jar");
compositeBundle.start();
if(compositeBundle.getState()== compositeBundle.ACTIVE)
System.out.println("Composite Bundle is Active!");


//HERE INSTALLING AND STARTING MY BUNDLE!!
Bundle b = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloIPojo_1.0.0.201401211340.jar");
b.start();

try {

ComponentType type = new PrimitiveComponentType()
.setBundleContext(b.getBundleContext())
.setComponentTypeName("hello.type")
.setClassName("helloipojo.HelloServiceImpl")
.setImmediate(true);
type.start();

ComponentInstance instance = type.createInstance();



}

catch (UnacceptableConfiguration
| MissingHandlerException | ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Create the instance







System.out.println("done starting bundles!");


} catch (BundleException e) {

e.printStackTrace();

System.out.println("Not done!");
}



//shutdownApplication();

}

public Bundle[] getInstalledBundles()
{
// Use the system bundle activator to gain external
// access to the set of installed bundles.
return m_activator.getBundles();
}

public void shutdownApplication()
{
// Shut down the felix framework when stopping the
// host application.
try {
m_felix.stop();
} catch (BundleException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
m_felix.waitForStop(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

当我运行我的应用程序时,它显示以下输出(末尾有错误):

6
Core Bundle is Active!
API Bundle is Active!
Composite Bundle is Active!
Bundle Started!
Exception in thread "main" java.lang.IllegalStateException: The factory associated with the component type is invalid (not started or missing handlers)
at org.apache.felix.ipojo.api.ComponentType.ensureFactory(ComponentType.java:189)
at org.apache.felix.ipojo.api.ComponentType.ensureAndGetFactory(ComponentType.java:177)
at org.apache.felix.ipojo.api.ComponentType.createInstance(ComponentType.java:79)
at HostApplication.<init>(HostApplication.java:109)
at Embedder.main(Embedder.java:11)

我哪里出错了?谢谢。

更新1

我可以看到我缺少 2 个处理程序。我通过添加以下两行知道这一点:

 System.out.println(type.getFactory().getRequiredHandlers());
System.out.println(type.getFactory().getMissingHandlers());

上面两行的输出是:

[org.apache.felix.ipojo:architecture, org.apache.felix.ipojo:callback]
[org.apache.felix.ipojo:architecture, org.apache.felix.ipojo:callback]

我也尝试过:

type.getFactory().createComponentInstance(new Properties());

然后我得到:

org.apache.felix.ipojo.MissingHandlerException: Missing handlers : org.apache.felix.ipojo:architecture org.apache.felix.ipojo:callback 

我不知道为什么这些处理程序丢失了。我尝试添加它们,但无法找出正确的语法。有什么帮助吗?谢谢。

更新2

根据克莱门特在his中的说法答案,我的包应该导入: org.apache.felix.ipojoorg.apache.felix.ipojo.architecture

我这样做了,现在我收到以下错误:

java.lang.ClassCastException: org.apache.felix.ipojo.HandlerManagerFactory cannot be cast to org.apache.felix.ipojo.HandlerFactory

我在这一行收到错误:type.start();

请帮忙。谢谢!

最佳答案

问题来自于 iPOJO 的异步启动。当您创建实例时,并非所有内容都可用。

我有几个问题:为什么使用 iPOJO API 来声明类型和实例?不能只用注释吗?在这种情况下,它就会顺利地创建一切。

如果您确实需要/想要使用该 API,请不要像您那样创建实例,而是公开一个实例声明:http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/ipojo-advanced-topics/ipojo-factory-service.html#deleting-instances 。声明会等到工厂有效才能创建实例。

关于java - IPOJO - 与组件类型关联的工厂无效(未启动或缺少处理程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com