java.lang.ClassCastException : ihtika2. i_testbundle.TestClasss 无法转换为 ihtika2.i_testbundle.service.TestClasssInter

标签 java osgi bundle apache-felix

我正在玩 Felix,但我无法理解一件事。 我有一些 OSGi Felix 包,我尝试从这个包加载和使用服务。 bundle 代码:

 package ihtika2.i_testbundle;

import ihtika2.i_testbundle.service.TestClasssInter;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

public class Activator implements BundleActivator {

    @Override
    public void start(BundleContext context) throws Exception {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("Funct", "TESTCl");

        context.registerService(TestClasssInter.class.getName(), new TestClasss(), props);


        ServiceReference[] refs;
        try {
            BundleContext bundleContext = context;

//            System.out.println(TestClasssInter.class.getName());
            refs = bundleContext.getServiceReferences("ihtika2.i_testbundle.service.TestClasssInter", "(Funct=TESTCl)");

            if (refs == null) {
                System.out.println("Not Found AboutForm on show!!!");
            } else {
                Object MainForm = bundleContext.getService(refs[0]);
                TestClasssInter sdfsdf = (TestClasssInter) MainForm;
                sdfsdf.printSomeLine();
//                    MainForm.sendContext(bundleContext);
//                    MainForm.showWindow();
            }

        } catch (InvalidSyntaxException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        // TODO add deactivation code here
    }
}

package ihtika2.i_testbundle;

import ihtika2.i_testbundle.service.TestClasssInter;

/**
 *
 * @author Arthur
 */
public class TestClasss implements TestClasssInter {

    @Override
    public void printSomeLine() {
        System.out.println("TEST MESSAGE");
    }
}

package ihtika2.i_testbundle.service;

/**
 *
 * @author Arthur
 */
public interface TestClasssInter {

    public void printSomeLine();
}

正如您在更新的示例中看到的那样 - 必须显示 bundle 代码中的“TEST MESSAGE”行。它的显示,一切正常。

但是如果我尝试在我的“加载程序”中执行此代码,则会显示错误

Could not create framework: java.lang.ClassCastException: ihtika2.i_testbundle.TestClasss cannot be cast to ihtika2.i_testbundle.service.TestClasssInter
java.lang.ClassCastException: ihtika2.i_testbundle.TestClasss cannot be cast to ihtika2.i_testbundle.service.TestClasssInter
    at com.google.code.ihtika.Starter.main(Starter.java:103)
Java Result: -1

加载器的代码是

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.ihtika;

import ihtika2.i_testbundle.service.TestClasssInter;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

/**
 * This class provides a static {@code main()} method so that the bundle can be
 * run as a stand-alone host application. In such a scenario, the application
 * creates its own embedded OSGi framework instance and interacts with the
 * internal extensions to providing drawing functionality. To successfully
 * launch the stand-alone application, it must be run from this bundle's
 * installation directory using "{@code java -jar}". The locations of any
 * additional extensions that have to be started, have to be passed as command
 * line arguments to this method.
 */
public class Starter {

    private static Framework m_framework = null;

    /**
     * Enables the bundle to run as a stand-alone application. When this static
     * {@code main()} method is invoked, the application creates its own
     * embedded OSGi framework instance and interacts with the internal
     * extensions to provide drawing functionality. To successfully launch as a
     * stand-alone application, this method should be invoked from the bundle's
     * installation directory using "{@code java -jar}". The location of any
     * extension that shall be installed can be passed as parameters. <p> For
     * example if you build the bundles inside your workspace, maven will create
     * a target directory in every project. To start the application from within
     * your IDE you should pass: <p>
     * <pre>
     * {@code file:../servicebased.circle/target/servicebased.circle-1.0.0.jar
     * file:../servicebased.square/target/servicebased.square-1.0.0.jar
     * file:../servicebased.triangle/target/servicebased.triangle-1.0.0.jar}
     * </pre>
     *
     * @param args The locations of additional bundles to start.
     *
     */
    public static void main(String[] args) {
        // Args should never be null if the application is run from the command line.
        // Check it anyway.
        ArrayList<String> locations = new ArrayList<>();

        indexBundlesDir("I_Bundles/Stage_300", locations);
        indexBundlesDir("I_Bundles/Stage_400", locations);
        indexBundlesDir("I_Bundles/Stage_500", locations);

        // Print welcome banner.
        System.out.println("\nWelcome to My Launcher");
        System.out.println("======================\n");

        try {
            Map<String, String> config = ConfigUtil.createConfig();
            m_framework = createFramework(config);
            m_framework.init();
            m_framework.start();
            installAndStartBundles(locations);

            for (Bundle testBundle : m_framework.getBundleContext().getBundles()) {
                if (testBundle.getSymbolicName().equals("ihtika2.I_TestBundle")) {
                    System.out.println("found");

                    ServiceReference[] refs;
                    try {
                        BundleContext bundleContext = m_framework.getBundleContext();

//                        System.out.println(TestClasssInter.class.getName());
                        refs = bundleContext.getServiceReferences("ihtika2.i_testbundle.service.TestClasssInter", "(Funct=TESTCl)");

                        if (refs == null) {
                            System.out.println("Not Found AboutForm on show!!!");
                        } else {
                            Object MainForm = bundleContext.getService(refs[0]);
                            TestClasssInter sdfsdf = (TestClasssInter) MainForm;
//                    MainForm.sendContext(bundleContext);
//                    MainForm.showWindow();
                        }

                    } catch (InvalidSyntaxException ex) {
                        ex.printStackTrace();
                    }

                }
//                Dictionary<String, String> headerLine = testBundle.getHeaders();
//                Enumeration e = headerLine.keys();
//
//                while (e.hasMoreElements()) {
//                    Object key = e.nextElement();
//                    if (key.equals("Import-Package")) {
//                        System.out.println(key + " - " + headerLine.get(key));
//                    }
//                    System.out.println(key + " - " + headerLine.get(key));
//                }
            }

            m_framework.waitForStop(0);
            System.exit(0);
        } catch (Exception ex) {
            System.err.println("Could not create framework: " + ex);
            ex.printStackTrace();
            System.exit(-1);
        }
    }

    private static void indexBundlesDir(String bundlesDir, ArrayList<String> locations) {
        File dir = new File(bundlesDir);
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            for (int i = 0; i < children.length; i++) {
                // Get filename of file or directory
                locations.add("file:/c:/Art/Dropbox/OpenSource/MyGIT/ihtika-2/ihtika-2/MainApplication/" + bundlesDir + "/" + children[i]);
            }
        }
    }

    /**
     * Util method for creating an embedded Framework. Tries to create a
     * {@link FrameworkFactory} which is then be used to create the framework.
     *
     * @param config the configuration to create the framework with
     * @return a Framework with the given configuration
     */
    private static Framework createFramework(Map<String, String> config) {
        ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class);
        for (FrameworkFactory factory : factoryLoader) {
            return factory.newFramework(config);
        }
        throw new IllegalStateException("Unable to load FrameworkFactory service.");
    }

    /**
     * Installs and starts all bundles used by the application. Therefore the
     * host bundle will be started. The locations of extensions for the host
     * bundle can be passed in as parameters.
     *
     * @param bundleLocations the locations where extension for the host bundle
     * are located. Must not be {@code null}!
     * @throws BundleException if something went wrong while installing or
     * starting the bundles.
     */
    private static void installAndStartBundles(ArrayList<String> bundleLocations) throws BundleException {
        BundleContext bundleContext = m_framework.getBundleContext();
//        Activator bundleActivator = new Activator();
//        bundleActivator.start(bundleContext);
        for (String location : bundleLocations) {
            Bundle addition = bundleContext.installBundle(location);
//            System.out.println(location);
            addition.start();
        }
    }
}

package ihtika2.i_testbundle.service;

/**
 *
 * @author Arthur
 */
public interface TestClasssInter {

    public void printSomeLine();
}

最佳答案

ClassLoader 并不是那样工作的,它确实需要相同的类/接口(interface)。 MainForm 实现 MainFormInterface,而不是 MainFormInterface2,即使它们是相同的。

你需要做的是:

  • 确保您的 MainFormInterface 在单独的包中(我认为它是:ihtika2.mainform.service)
  • 删除 MainFormInterface2
  • 将所有 MainFormInterface2 引用替换为 MainFormInterface
  • 将包添加到 Felix 的 org.osgi.framework.system.packages.extra 设置中,我认为最简单的地方就是在 ConfigUtil.createConfig() 之后将它添加到 map 中,这样 Felix 就可以访问从 OSGi 外部到 ihtika2.mainform.service 包
  • 确保您的包导入包 ihtika2.mainform.service,这样您的包也可以访问 ihtika2.mainform.service

应该这样做。

关于java.lang.ClassCastException : ihtika2. i_testbundle.TestClasss 无法转换为 ihtika2.i_testbundle.service.TestClasssInter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11734655/

相关文章:

java - 如何知道 HttpServletResponse 重定向到哪里?

java - felix exthttpservice 设置 cookie 的 session 路径

java - Apache 菲利克斯 6.0.1 : BundleException on init

android - 保存的实例包在 DialogFragment 的 onCreateView 中为 null

python - 用我的项目创建 Python pip 包

java - ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 com.android.internal.widget.ActionBarOverlayLayout$LayoutParams

java - 在 GWT 应用程序中使用 RMI

java - 向 Activity 发送包数据的 Android 服务为 NULL

java - Saga 条目表/Sagas 集合保持为空

java - OSGi 蓝图配置 : injecting a list of bean references