본문 바로가기

JAVA

[ OSGi ] 4 - Spring DM 1

728x90


[ OSGi ] 3 - OSGi plugin 의존 관계 에서 서로 다른 Plugin 간의 Class 사용을 해보았다. ( Import-Package, Export-Package )

그런데 이런 단순한 내용 뿐만 아니라 Spring의 핵심 기능인 DI는 IoC중의 한 종류인데 OSGi framework 에서도 IoC를 설계하여 적용 할수 있다.

바로 OSGi의 서비스 등록인데 

이는 http://xguru.net/tag/osgi 에서 OSGi 시작하기 4OSGi 사직하기 5 에 잘 정리 되어 있다. 

꼭 읽어봐야함!!! 



여기서는 OSGi에서의 Spring DM을 이용한 서비스 등록을 보도록 하겠다. 

Spring DM을 할용해서 OSGi 서비스의 등록을 간편하게 할 수 있다. 


< 참고 >

Spring DM은 현재 spring.io 더이상 진행 하지 않는다.

spring.io에서 eclipse.org에게 SpringDM Project을 기부하면서 이름이 blueprint로 바뀌었다. 

여기서는 예전의 SpringDM을 사용해서 진행을 하도록 하겠다.


Spring을 OSGi에서 사용하기 위해서는 OSGi에서 사용할수 있는 Spring bundle 이 필요한데 Spring의 최신 버전은 OSGi Bundle로 사용할수 있게 더이상 module을 제공하지 않는다.

하지만 servicemix 라는 곳에서 이러한 Spring을 버전별로 OSGi bundle에서 사용할수 있도록 수정하여 maven repository를 제공하고 있다.

http://mvnrepository.com/artifact/org.apache.servicemix.bundles

여기에는 Spring 뿐만 아니라 OSGi에서 사용할수 있도록 각종 library를 제공하고 있다.


이 글을 쓰는 시점에는 Spring 4.0.5 version 까지 제공하고있다. ( Spring은 4.0.6 까지 배포됨. )


이 글에서는 Spring 3.2.9 RELEASE를 이용 한다.


필요 Library

1. spring 관련 libary 추가 

Spring DM을 쓰기 위해서는 다음과 같은 library가 기본으로 필요하다.

com.springsource.org.aopalliance-1.0.0.jar

commons-logging-1.1.3.jar

log4j-1.2.17.jar

org.apache.servicemix.bundles.spring-aop-3.2.9.RELEASE_1.jar

org.apache.servicemix.bundles.spring-beans-3.2.9.RELEASE_1.jar

org.apache.servicemix.bundles.spring-context-3.2.9.RELEASE_1.jar

org.apache.servicemix.bundles.spring-context-support-3.2.9.RELEASE_1.jar

org.apache.servicemix.bundles.spring-core-3.2.9.RELEASE_1.jar

org.apache.servicemix.bundles.spring-expression-3.2.9.RELEASE_1.jar

slf4j-api-1.7.7.jar

slf4j-log4j12-1.7.7.jar

spring-osgi-annotation-1.2.1.jar

spring-osgi-core-1.2.1.jar

spring-osgi-extender-1.2.1.jar

spring-osgi-io-1.2.1.jar

Library 첨부 : 

spring_lib.zip


해당 library 를 plugins 밑에 "spring" folder를 만들어서 넣어두자


최종적인 모습은 다음과 같다.

sample 

   |-- plugins

            |-- org.apache.felix.gogo.command_xxx.jar

            |-- org.apache.felix.gogo.runtime_xxx.jar

            |-- org.apache.felix.gogo.shell_xxx.jar

            |-- org.eclipse.equinox.common_xxx.jar

            |-- org.eclipse.equinox.console_xxx.jar

            |-- org.eclipse.osgi_xxx.jar

            |-- sample.HelloWorldOSGi_1.0.0.jar

            |-- sample.HelloWorldOSGiSub_1.0.0.jar

            |-- spring

                     |-- com.springsource.org.aopalliance-1.0.0.jar

                     |-- commons-logging-1.1.3.jar

                     |-- log4j-1.2.17.jar

                     |-- org.apache.servicemix.bundles.spring-aop-3.2.9.RELEASE_1.jar

                     |-- org.apache.servicemix.bundles.spring-beans-3.2.9.RELEASE_1.jar

                     |-- org.apache.servicemix.bundles.spring-context-3.2.9.RELEASE_1.jar

                     |-- org.apache.servicemix.bundles.spring-context-support-3.2.9.RELEASE_1.jar

                     |-- org.apache.servicemix.bundles.spring-core-3.2.9.RELEASE_1.jar

                     |-- org.apache.servicemix.bundles.spring-expression-3.2.9.RELEASE_1.jar

                     |-- slf4j-api-1.7.7.jar

                     |-- slf4j-log4j12-1.7.7.jar

                     |-- spring-osgi-annotation-1.2.1.jar

                     |-- spring-osgi-core-1.2.1.jar

                     |-- spring-osgi-extender-1.2.1.jar

                     |-- spring-osgi-io-1.2.1.jar

   |-- configuration

            |-- config.ini 


2. config.ini를 다음과 같이 수정한다.

#Eclipse Runtime Configuration File

osgi.bundles=org.eclipse.equinox.common_3.6.200.v20130402-1505.jar@2:start,\

org.eclipse.equinox.console_1.1.0.v20140131-1639.jar@start,\

org.apache.felix.gogo.shell_0.10.0.v201212101605.jar@start,\

org.apache.felix.gogo.command_0.10.0.v201209301215.jar@start,\

org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar@start,\

spring/com.springsource.org.aopalliance-1.0.0.jar@start,\

spring/commons-logging-1.1.3.jar@start,\

spring/log4j-1.2.17.jar@start,\

spring/org.apache.servicemix.bundles.spring-aop-3.2.9.RELEASE_1.jar@start,\

spring/org.apache.servicemix.bundles.spring-beans-3.2.9.RELEASE_1.jar@start,\

spring/org.apache.servicemix.bundles.spring-context-3.2.9.RELEASE_1.jar@start,\

spring/org.apache.servicemix.bundles.spring-context-support-3.2.9.RELEASE_1.jar@start,\

spring/org.apache.servicemix.bundles.spring-core-3.2.9.RELEASE_1.jar@start,\

spring/org.apache.servicemix.bundles.spring-expression-3.2.9.RELEASE_1.jar@start,\

spring/slf4j-api-1.7.7.jar@start,\

spring/slf4j-log4j12-1.7.7.jar,\

spring/spring-osgi-annotation-1.2.1.jar@start,\

spring/spring-osgi-core-1.2.1.jar@start,\

spring/spring-osgi-extender-1.2.1.jar@start,\

spring/spring-osgi-io-1.2.1.jar@start

osgi.bundles.defaultStartLevel=4


3. OSGi 실행 후 ss 명령어를 입력하면 다음과같이 나오면 성공한 것이다.

C:\develop\OSGi\sample\plugins>java -Declipse.ignoreApp=true -jar org.eclipse.osgi_3.10.0.v20140606-1445.jar -console -consoleLog

log4j:WARN No appenders could be found for logger (org.springframework.osgi.exte

nder.internal.activator.ContextLoaderListener).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

osgi> ss

"Framework is launched."



id      State       Bundle

0       ACTIVE      org.eclipse.osgi_3.10.0.v20140606-1445

1       ACTIVE      org.eclipse.equinox.common_3.6.200.v20130402-1505

2       ACTIVE      org.eclipse.equinox.console_1.1.0.v20140131-1639

3       ACTIVE      org.apache.felix.gogo.shell_0.10.0.v201212101605

4       ACTIVE      org.apache.felix.gogo.command_0.10.0.v201209301215

5       ACTIVE      org.apache.felix.gogo.runtime_0.10.0.v201209301036

6       ACTIVE      com.springsource.org.aopalliance_1.0.0

7       ACTIVE      org.apache.commons.logging_1.1.3

8       ACTIVE      log4j_1.2.17

9       ACTIVE      org.apache.servicemix.bundles.spring-aop_3.2.9.RELEASE_1

10      ACTIVE      org.apache.servicemix.bundles.spring-beans_3.2.9.RELEASE_1

11      ACTIVE      org.apache.servicemix.bundles.spring-context_3.2.9.RELEASE_1


12      ACTIVE      org.apache.servicemix.bundles.spring-context-support_3.2.9.RELEASE_1

13      ACTIVE      org.apache.servicemix.bundles.spring-core_3.2.9.RELEASE_1

14      ACTIVE      org.apache.servicemix.bundles.spring-expression_3.2.9.RELEASE_1

15      ACTIVE      slf4j.api_1.7.7

                    Fragments=16

16      RESOLVED    slf4j.log4j12_1.7.7

                    Master=15

17      ACTIVE      org.springframework.osgi.extensions.annotations_1.2.1

18      ACTIVE      org.springframework.osgi.core_1.2.1

19      ACTIVE      org.springframework.osgi.extender_1.2.1

20      ACTIVE      org.springframework.osgi.io_1.2.1

osgi> 


제대로 나오지 않으면 "plugins\configuration 폴더"에서 "config.ini"를 제외하고 모든 폴더와 파일을 지우고 다시 한번 해보자.





프로젝트 생성

다음과 같이 프로젝트를 생성한다.



이번에는 Options의 "Generate an activator a Java class that controls the plug-in's life cycle" 체크 박스를 선택하지 않는다.



프로젝트를 생성후 Spring 프로젝트로 변경을 해준다.



kr.co.jabsiri.helloworldosgi.springdm.HelloWorldOSGiSpringDMStarter.java 를 만들고 다음과 같이 작성한다. 



이 클래스가 이전에 봐왔던 Activator를 역할을 대신 하게 된다. 

META-INF 폴더 밑에 spring.xml 파일을 만들고 다음과 같이 작성한다.



spring.xml 내용

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:osgi="http://www.springframework.org/schema/osgi"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/osgi

        http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<bean id="helloOSGiSpringDM" 

class="kr.co.jabsiri.helloworldosgi.springdm.HelloWorldOSGiSpringDMStarter" 

init-method="start" destroy-method="stop" />

</beans>

init-method 는 activator의 start와 역활을 하는 메소드를 지정하며

destroy-method는 activator의 stop 역활을 하는 메소드를 지정한다. 


배포 

프로젝 생성에 코드까지 생성 했으면 배포후 install을 해보자 ( 배포는 앞의 강좌를 참고 )

osgi> install file:HelloWorldOSGiSpringDM_1.0.0.jar

Bundle id is 21

Location             file:HelloWorldOSGiSpringDM_1.0.0.jar

State                2

Bundle                  21|Installed  |    1|HelloWorldOSGiSpringDM (1.0.0)

Version              1.0.0

LastModified         1409042885287

Headers               Bundle-ManifestVersion = 2

 Bundle-Name = HelloWorldOSGiSpringDM

 Bundle-RequiredExecutionEnvironment = JavaSE-1.6

 Bundle-SymbolicName = HelloWorldOSGiSpringDM

 Bundle-Version = 1.0.0

 Manifest-Version = 1.0



BundleContext        null

BundleId             21

SymbolicName         HelloWorldOSGiSpringDM

RegisteredServices   null

ServicesInUse        null

Module               osgi.identity; osgi.identity="HelloWorldOSGiSpringDM"; type="osgi.bundle"; version:Version="1.0.0" [id=21]


osgi> start 21

osgi> spring dm start


osgi> stop 21

spring dm stop

osgi> 







첨부 1 : HelloWorldOSGi plugin Eclipse Project

HelloWorldOSGi_4.zip



첨부 2 : OSGi Framework

plugins_osgi_4.zip




'JAVA' 카테고리의 다른 글

[ OSGi ] 6 - Spring DM 3  (0) 2014.08.28
[ OSGi ] 5 - Spring DM 2  (0) 2014.08.26
[ OSGi ] 3 - OSGi plugin 의존 관계  (0) 2014.08.20
[ OSGi ] 2 - OSGi plugin project  (2) 2014.08.19
[ OSGi ] 1 - OSGi 기초  (0) 2014.06.24

태그