glassfish rest administration backend

39
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Upload: arun-gupta

Post on 19-Jun-2015

1.781 views

Category:

Technology


2 download

DESCRIPTION

GlassFish REST Administration Backend at JavaOne Latin America 2011

TRANSCRIPT

Page 1: GlassFish REST Administration Backend

1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Page 2: GlassFish REST Administration Backend

2 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

GlassFish REST Administration Back End An Insider Look at a Real REST Application

John Clingan, Principal Product Manager, Oracle

Page 3: GlassFish REST Administration Backend

3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. 3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Page 4: GlassFish REST Administration Backend

4 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 5: GlassFish REST Administration Backend

5 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 6: GlassFish REST Administration Backend

6 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background

•  JAX-RS – Standard annotation-driven API that aims to help developers build

RESTful Web services in Java –  JSR 311: JAX-RS 1.x released Nov 2009

•  Jersey – Reference implementation of JAX-RS – Ships with GlassFish

JAX-RS

Page 7: GlassFish REST Administration Backend

7 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background

1 @Path("myresource") 2 @Produces({"application/html, application/xml, application/json"}) 3 public class MyResource { 4 5 @GET /* curl -X GET http://example.com/myapp/myresource */6 public MyResource getMyResource() { . . . } 7 8 @Path("{Name}/") /* curl -X GET http://example.com/myapp/myresource/someChildName */ 9 public ChildResource getMyChildResource(@PathParam("Name") String name) { . . .} 10 11 @POST /* curl -X POST -d "attr1=value1:attr2=value2..." http://example.com/myapp/myresource */12 public Response createResource(HashMap<String, String> data) { . . . } 13 14 @DELETE /* curl -X DELETE http://.../myapp/myresource */ 15 public Response deleteResource() { . . . } 16 }

Sample JAX-RS resource

Page 8: GlassFish REST Administration Backend

8 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS

•  GlassFish •  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 9: GlassFish REST Administration Backend

9 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background

–  Java EE reference implementation – Highly modular

• Leverages OSGI • Every container/service is a set of OSGI modules • Multiple distributions

–  Java EE Web Profile. –  Java EE Full Profile

• Extensible – add custom modules – Administration using CLI, Console, REST API

GlassFish

Page 10: GlassFish REST Administration Backend

10 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background GlassFish Modular Architecture

Java SE

OSGI Runtime

HK2

Config Framework

CLI Framework Grizzly Monitoring

Framework REST

Backend

GlassFish Nucleus Security Service

Web Profile Distribution Web Container Deployment Java

Persistence Transaction

Service Naming Service

EJB Lite Container JDBC Service

EJB Container MDB Container JMS Service

Full Profile Distribution Java Mail ACC CMP

Page 11: GlassFish REST Administration Backend

11 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background

•  domain.xml file – Persistence storage for configuration – Dynamic structure and content.

• GlassFish is highly modular •  Defined at run time depending on modules installed

GlassFish Configuration

Page 12: GlassFish REST Administration Backend

12 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Background 1 <domain log-root="${com.sun.aas.instanceRoot}/logs" ... > 2 <applications>...</applications> 3 <resources> 4 <jdbc-connection-pool datasource-classname="..." res-type="javax.sql.XADataSource" name="__TimerPool"> 5 <property name="connectionAttributes" value=";create=true"></property> 6 ... 7 </jdbc-connection-pool> 8 <jdbc-connection-pool datasource-classname="..." res-type="javax.sql.XADataSource" name="MyPool"> 9 <property name="MyProperty" value="MyValue"></property> 10 </jdbc-connection-pool> 11 ... 12 <jdbc-resource pool-name="TimerPool" jndi-name="jdbc/__TimerPool" object-type="system-admin"></jdbc-resource> 13 ... 14 </resources> 15 <configs> 16 <config name="server-config"> 17 <http-service> ... </http-service> 18 <ejb-container session-store="${com.sun.aas.instanceRoot}/session-store"> 19 <ejb-timer-service>...</ejb-timer-service> 20 </ejb-container> 21 ... 22 </config> 23 ... 24 </configs> 25 ... 26 </domain>"

GlassFish domain.xml snippet

Page 13: GlassFish REST Administration Backend

13 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 14: GlassFish REST Administration Backend

14 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

•  Annotated POJOs •  Each module uses Config Beans to define its

configuration

GlassFish Config Beans

Page 15: GlassFish REST Administration Backend

15 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

@Configured"public interface JdbcConnectionPool extends ConfigBeanProxy {" @Attribute(key=true) " String getName(); " void setName(String value);"

@Attribute " String getDatasourceClassname();" ..." @Element " List<Property> getProperty();" ..."}"

Example Config Bean

Page 16: GlassFish REST Administration Backend

16 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

@Configured"public interface JdbcConnectionPool extends ConfigBeanProxy {" @Attribute(key=true) " String getName(); " @Attribute " String getDatasourceClassname();" ..." @Element " List<Property> getProperty();" ..."}"

Corresponding snippet from domain.xml <jdbc-connection-pool name=" ... " datasource-classname=" ... " >" <property name="..." value="..."/ > " <property name="..." value="..."/ > "</jdbc-connection-pool>"

Example Config Bean

Page 17: GlassFish REST Administration Backend

17 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

•  The gateway to manipulate GlassFish configuration –  Transaction – Replication

•  Examples – Create a jdbc connection pool

$ asadmin create-jdbc-connection-pool \ --datasourceclassname oracle.jdbc.pool.OracleDataSource \ --property User=scott:Password=tiger:URL=jdbc:oracle:thin ...\"

MyOraclePool"

– Delete a jdbc connection pool $ asadmin delete-jdbc-connection-pool MyOraclePool"

GlassFish Command Line Interface (CLI)

Page 18: GlassFish REST Administration Backend

18 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

1 @org.glassfish.api.admin.ExecuteOn(RuntimeType.ALL) 2 @Service(name="create-jdbc-connection-pool") 3 ... 4 public class CreateJdbcConnectionPool implements AdminCommand { 5 @Param(name="jdbc_connection_pool_id", alias = "name", primary=true) 6 String jdbc_connection_pool_id; 7 8 @Param(name = "datasourceClassname", optional=true) 9 String datasourceclassname; 10 11 @Param(name="property", optional=true, separator=':') 12 Properties properties; 13 14 public void execute(AdminCommandContext context) { 15 create_internal_data_structures(); 16 persist_into_configuration(); 17 } 18 }

GlassFish Command Line Interface (CLI)

Page 19: GlassFish REST Administration Backend

19 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

• Management – Access to configuration – GET to access and navigate

configuration tree – POST/DELETE to mutate configuration

• Wraps CLI to mutate configuration objects

GlassFish admin REST backend

GlassFish Server Instance

REST Backend

Config Beans

domain.xml

GlassFish CLI GET

POST DELETE

Page 20: GlassFish REST Administration Backend

20 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

• Monitoring – Data available as a tree in

running GlassFish VM – GET to access and navigate

the tree

GlassFish admin REST backend

GlassFish Server Instance

REST Backend

Monitoring Tree

GET

Page 21: GlassFish REST Administration Backend

21 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

• Demo –  Lets see REST backend in action

GlassFish admin REST backend

Page 22: GlassFish REST Administration Backend

22 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details 1 public class ListJdbcConnectionPoolResource { 2 3 @Context protected ResourceContext resourceContext; 4 5 @GET 6 public Response get() { 7 return Response.ok().entity(get_list_of_jdbc_resources()).build() 8 } 9 10 @POST 11 public Response createResource(HashMap<String, String> data) { 12 RestActionReporter actionReport = ResourceUtil.runCommand(getPostCommand(), data, ...); 13 return Response.ok(arr).build(); 14 } 15 16 @Path("{Name}/") 17 public JdbcConnectionPoolResource getJdbcConnectionPoolResource(@PathParam("Name") String id){ 18 JdbcConnectionPoolResource resource = 19 resourceContext.getResource(JdbcConnectionPoolResource.class); 20 resource.setBeanByKey(entity, id); 21 return resource; 22 } 23 }"

GlassFish Config Bean as REST resource

Page 23: GlassFish REST Administration Backend

23 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details 1 public class JDBCConnectionPoolResource {"2 ""3 @GET"4 public Response getEntity() {"5 return Response.ok(get_content_of_current_config_bean() )"6 } "7"8 @POST"9 public Response updateEntity(HashMap<String, String> data) {"10 RestActionReporter ar = Util.applyChanges(data, uriInfo, habitat); // calls asadmin set"11 return Response.ok( 12 ResourceUtil.getActionReportResult(ar, successMessage, requestHeaders, uriInfo)).build();"13 }"14"15 @DELETE"16 public Response delete(HashMap<String, String> data) {"17 RestActionReporter actionReport = runCommand(getDeleteCommand(), data);"18 }"19"

20 }"

GlassFish Config Bean as REST resource

Page 24: GlassFish REST Administration Backend

24 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

• Generate REST resources dynamically – Visit each bean of GlassFish config model to drive generation

• Bytecode –  Uses ASM

• Text – For development time debugging • Report – Generating usage report

– Abstract out most of the logic in handwritten base class – REST enable newly added module transparently

Code Generation

Page 25: GlassFish REST Administration Backend

25 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

•  Lets go through real code to see this in action Demo

Page 26: GlassFish REST Administration Backend

26 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Implementation Details

•  Initialized lazily – No startup penalty

• No dependency on servlet container – Using Grizzly adapter

Bootstrapping

Page 27: GlassFish REST Administration Backend

27 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 28: GlassFish REST Administration Backend

28 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Tips And Tricks

• GlassFish REST interface is used by – Users of GlassFish.

• Prefer HTML – GlassFish Admin GUI and other programmatic consumption

• Prefer JSON/XML

• @Provider for various formats –  ActionReport[Html|Json|Xml]Provider

•  com.sun.jersey….ResourceConfig#MediaTypeMapping – Add mediatype mapping for “.xml” “.json” and “.html” for content

negotiation

Enable Efficient Programmatic and Manual Consumption

Page 29: GlassFish REST Administration Backend

29 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Tips And Tricks

•  Enables modularity – Associate commands that manipulate a config bean

@Configured "

@RestRedirects({"

@RestRedirect(opType = RestRedirect.OpType.POST, commandName = "create-jdbc-resource"),"

@RestRedirect(opType = RestRedirect.OpType.DELETE, commandName = "delete-jdbc-resource")"

})"

"public interface JdbcResource extends ConfigBeanProxy, Resource, .. { . . . }"

– Place a command at certain position in tree @Service(name = "copy-config")"

@RestEndpoints({"

@RestEndpoint(configBean=Configs.class, opType=OpType.POST, path="copy-config",..)"

})"

public final class CopyConfigCommand ...{ ... }

Annotate domain model

Page 30: GlassFish REST Administration Backend

30 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Tips And Tricks

• REST is stateless – Authentication info travels with every request

• GlassFish Admin GUI console (Client) needs to cache credentials • Security Risk if it caches userId + passWord

•  Authentication tokens – Self expiring – Admin GUI requests a token with user supplied id and password – Subsequent request only need to use the token

Security

Page 31: GlassFish REST Administration Backend

31 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Tips and Tricks

• GlassFish Monitoring – Data collected on each

instance of cluster

•  Proxy instance data through DAS – DAS proxies request to

target instance – Provides transparency – Handles security

Proxy-ing

Instance1 REST Backend

Monitoring Tree

DAS REST Backend

Monitoring Tree

Instance2 REST Backend

Monitoring Tree

GET instance1 data

GET instance2 data

GET GET

Page 32: GlassFish REST Administration Backend

32 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 33: GlassFish REST Administration Backend

33 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Clients Of GlassFish REST backend

• GlassFish Admin GUI console • GlassFish NetBean plugin • GlassFish Eclipse plugin • GlassFish.next

– GlassFish PaaS console

•  End Users – Browser – Demo from Adam Bien

Page 34: GlassFish REST Administration Backend

34 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Program Agenda

•  Background –  JAX-RS – GlassFish

•  Implementation details •  Tips and Tricks • Clients •  Future plans • Q & A

Page 35: GlassFish REST Administration Backend

35 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Future Plans

•  Provide client side library with various language bindings •  Backend of PaaS console for GlassFish.next •  Leverage planned work in Jersey to define REST

resources without generating classes •  Improve HTML interface •  Internal clean up

Page 36: GlassFish REST Administration Backend

36 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Resources

•  GlassFish –  home page - http://glassfish.java.net/ –  REST backend source code -

https://svn.java.net/svn/glassfish~svn/trunk/main/nucleus/admin/rest/rest-service/

•  JAX-RS –  JAX-RS 1.x - http://jcp.org/en/jsr/detail?id=311 –  JAX-RS 2.0 - http://jcp.org/en/jsr/detail?id=339 –  Jersey – http://jersey.java.net

Page 37: GlassFish REST Administration Backend

37 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Q&A

Page 38: GlassFish REST Administration Backend

38 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Tokyo 2012

April 4–6, 2012

Page 39: GlassFish REST Administration Backend

39 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.