ExoBrain 프로젝트
public class MobileOfficeSimulator {
public static void main(String[] args) {
/* create mobile office block 4 by 4 */
MobileOffice.createOfficeBlock(4, 4);
/* instantiate with environment */
OfficeBlock block = new OfficeBlockBuilder()
.xPos(0).yPos(0)
.brightness(60)
.humidity(55)
.temperature(25)
.createOfficeBlock();
MobileOffice.assignBlock(block);
MobileOffice.printCurrentState();
/* instantiate device & assign to mobile office */
AirCon aircon = new AirCon(0 , 0);
MobileOffice.addDevice(aircon);
/* operate device in duration time */
aircon.cool(3);
MobileOffice.printCurrentState();
System.out.println(MobileOffice.getSerializedJson());
}
}
Eclipse 역시 OSGi플랫폼 위에 구동되므로 새로운 타겟 플랫폼을 생성하여 기존의 IDE에 변경이 없는 개발 환경을 만들어야 합니다.
Eclipse 의 상단 메뉴 중 Preference - Plugin Development 에서 Target Platform에서
새로운 타겟 플랫폼을 추가합니다.
여타 다른 번들들은 필요하지 않으므로 Nothing 으로 플랫폼을 생성합니다.
개발시 필요한 OSGi - HTTP REST 를 지원하는 라이브러리를 추가합니다.
http://download.eclipse.org/eclipse/updates/4.3
를 추가하여 "Equinox Target Components" 를 설치합니다.
http://hstaudacher.github.io/osgi-jax-rs-connector
를 추가하여 "OSGi - JAX -RS Connector" 를 설치합니다. 그외 번들들은 기타 서비스를 이용할 때 필요합니다.
새 프로젝트를 생성할 때 Target 을 Equinox로 설정해줍니다.
프로젝트 생성 후 MANIFEST.MF 를 다음과 같이 JAX-RS API를 이용할 수 있게 설정해줍니다.
ExampleResource.java
/* annotation 을 통해 경로 지정 */
@Path( "/hello" )
public class ExampleResource {
@GET
public String helloWorld() {
return "Hello World";
}
}
Activator.java
public class Activator implements BundleActivator {
private ServiceRegistration<ExampleResource> registration;
public void start(BundleContext bundleContext) throws Exception {
/* 서비스 등록 */
registration = bundleContext.registerService(ExampleResource.class, new ExampleResource(), null);
}
public void stop(BundleContext bundleContext) throws Exception {
registration.unregister();
}
}
// Our Application osgi.rest.example
// The OSGI implementation org.eclipse.osgi org.eclipse.osgi.services
// The OSGi console org.eclipse.equinox.console org.apache.felix.gogo.command org.apache.felix.gogo.runtime org.apache.felix.gogo.shell
// The OSGi HttpService implementation org.eclipse.equinox.http.jetty org.eclipse.equinox.http.servlet org.eclipse.jetty.continuation org.eclipse.jetty.http org.eclipse.jetty.io org.eclipse.jetty.security org.eclipse.jetty.server org.eclipse.jetty.servlet org.eclipse.jetty.util javax.servlet-api
// The JAX-RS Connector com.eclipsesource.jaxrs.publisher
// Jersey com.eclipsesource.jaxrs.jersey-all (this is Jersey repackaged as a single bundle)
HTTP 서비스를 이용하기 위해 Run 혹은 Debug 설정에서 OSGi 실행 Argument에
-Dorg.osgi.service.http.port=포트 번호
를 추가해줍니다.
Ref : http://eclipsesource.com/blogs/2014/02/04/step-by-step-how-to-bring-jax-rs-and-osgi-together/