apache deltaspike the cdi toolbox (java one 2015)

53
Apache DeltaSpike - the CDI toolbox Antoine Sabot-Durand · Rafael Benevides

Upload: antoine-sabot-durand

Post on 21-Feb-2017

881 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Apache Deltaspike the CDI Toolbox (Java One 2015)

ApacheDeltaSpike-theCDItoolbox

AntoineSabot-Durand·RafaelBenevides

Page 2: Apache Deltaspike the CDI Toolbox (Java One 2015)

RafaelBenevides

DeltaSpikeP.M.CmemberRedHat,[email protected]/rafabene

Page 3: Apache Deltaspike the CDI Toolbox (Java One 2015)

AntoineSabot-Durand

CDIspecleadRedHat,Inc.@antoine_sdnext-presso.comgithub.com/antoinesd

Page 4: Apache Deltaspike the CDI Toolbox (Java One 2015)

ShouldIstayorshouldIgo?

AtalkaboutCDIeco-system

Don’tneedtobeaCDIguru

ButyouneedtoknowCDI

Page 5: Apache Deltaspike the CDI Toolbox (Java One 2015)

@Inject @ProducesEvent<T> @Observes@Qualifier InjectionPoint

ShouldIstayorshouldIgo?

Ifyouknowthemostoftheseyoucanstay

Page 6: Apache Deltaspike the CDI Toolbox (Java One 2015)

Agenda

WhatisDeltaSpike?CoreModuleOtherDeltaSpikeModulesQuestion&Answers

Slidesavailableatrafabene.github.io/deltaspike-cdi-toolbox/

Page 7: Apache Deltaspike the CDI Toolbox (Java One 2015)

WhatisDeltaSpike?

Page 8: Apache Deltaspike the CDI Toolbox (Java One 2015)

Wheredoesitcomefrom?

Page 9: Apache Deltaspike the CDI Toolbox (Java One 2015)

AbitofhistoryDec2011:projectlaunchFeb2012:version0.1May2013:version0.4(outofincubator)June2014:version1.0August2015:version1.5

Page 10: Apache Deltaspike the CDI Toolbox (Java One 2015)

CDI&DeltaSpike

CDIisaspecification.Itdoesn’tprovidebusinessfeatures

butitincludesapowerfulhooktoaddthesebusinessfeatures

The"Portableextensions"featureisthishook

Thankstoit,CDIcanbeeasilyenhancedwithnewhighlevelfeatures

Page 11: Apache Deltaspike the CDI Toolbox (Java One 2015)

CDIPortableextensions

OneofthemostpowerfulfeatureoftheCDIspecification

Notreallypopularized,partlydueto:

1. Theirhighlevelofabstraction2. ThegoodknowledgeonBasicCDIandSPI3. Lackofinformation(CDIisoftenreducedtoabasicDIsolution)

Page 12: Apache Deltaspike the CDI Toolbox (Java One 2015)

Extensions,whatfor?

Tointegrate3rdpartylibraries,frameworksorlegacycomponents

Tochangeexistingconfigurationorbehavior

ToextendCDIandJavaEE

Thankstothem,JavaEEcanevolvebetweenmajorreleases

Page 13: Apache Deltaspike the CDI Toolbox (Java One 2015)

Extensions,how?

ObservingSPIeventsatboottimerelatedtothebeanmanagerlifecycle

Checkingwhatmeta-dataarebeingcreated

Modifyingthesemeta-dataorcreatingnewones

Page 14: Apache Deltaspike the CDI Toolbox (Java One 2015)

Moreconcretely

Serviceprovideroftheservicejavax.enterprise.inject.spi.Extension declaredinMETA-INF/services

Justputthefullyqualifiednameofyourextensionclassinthisfile

import javax.enterprise.event.Observes;import javax.enterprise.inject.spi.Extension;

public class CdiExtension implements Extension {

void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { } //...

void afterDeploymentValidation(@Observes AfterDeploymentValidation adv) { }}

Page 15: Apache Deltaspike the CDI Toolbox (Java One 2015)

Internal Step Happen Once Loop on Elements

Beanmanagerlifecycle

DeploymentStart

BeforeBean

Discovery

ScanArchive

ProcessAnnotated

Type

AfterType

Discovery

BeanEligibility

Check

ProcessInjection

Point

ProcessInjectionTarget

ProcessBean

Attributes

ProcessBean

ProcessProducer

ProcessObserverMethod

AfterBean

Discovery

AfterDeploymentValidation

ApplicationRunning

BeforeShutdown

UndeployApplication

Page 16: Apache Deltaspike the CDI Toolbox (Java One 2015)

Example:IgnoringJPAentities

ThefollowingextensionpreventsCDItomanageentities

Thisisacommonlyadmittedgoodpractice

public class VetoEntity implements Extension {

void vetoEntity(@Observes @WithAnnotations(Entity.class) ProcessAnnotatedType<?> pat) { pat.veto(); }}

Page 17: Apache Deltaspike the CDI Toolbox (Java One 2015)

ExtensionsarelaunchedduringbootstrapandarebasedonCDIevents

Oncetheapplicationisbootstrapped,theBeanManagerisinread-onlymode(noruntimebeanregistration)

Youonlyhaveto@Observes built-inCDIeventstocreateyourextensions

Remember

Page 18: Apache Deltaspike the CDI Toolbox (Java One 2015)

ApacheDeltaSpikeis…

Acollectionofreadytouseextensionstohelpyouinyourprojects

AtoolboxtohelpyoudevelopnewCDIportableextensions

Agreatwaytolearnhowtodevelopyourownextensionbybrowsingthesourcecode

ThemostobviousentrypointtoCDIeco-system

Page 19: Apache Deltaspike the CDI Toolbox (Java One 2015)

DeltaSpikeistestedwithCDI1.0,1.1,1.2and2.0JBossWeldandApacheOpenWebBeansJBossAS7.x,WildFly8.x-10.xJBossEAP6.x-7.xApacheTomEE1.0.x-1.7.xOracleGlassFish3.x,4.xOracleWeblogic12cIBMWebsphere8.x

Page 20: Apache Deltaspike the CDI Toolbox (Java One 2015)

DeltaspikeisforallCDIdevelopers

Whilethistalkisfocusedonclassicalprojectdevelopers…

…advanceddevelopersmayfindinterestinghelpersinDeltaspike

Forinstancemetadatabuilderareusefulforportableextensiondevelopers

public void registerGenericBeans(@Observes AfterBeanDiscovery abd) { BeanBuilder<User> ubb = new BeanBuilder<User>(beanManager).readFromType(User.class) .passivationCapable(true) .addTypes(otherTypes); if (weAreOnWeb) ubb.scope(SessionScoped.class); else ubb.scope(ApplicationScoped.class); abd.addBean(ubb.create());}

Page 21: Apache Deltaspike the CDI Toolbox (Java One 2015)

MoreonDeltaSpike

ApacheDeltaSpikereceivedDuke’schoiceaward2014

Thistalkshowsonlyasmallpartoftheframework

Moreinfoon:deltaspike.apache.org/

Page 22: Apache Deltaspike the CDI Toolbox (Java One 2015)

Modulesanddependencies

Page 23: Apache Deltaspike the CDI Toolbox (Java One 2015)

CoreModule

Page 24: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-ExceptionHandlerpublic class InventoryActions { @PersistenceContext private EntityManager em;

@Inject private Event<ExceptionToCatchEvent> catchEvent;

public Integer queryForItem(Item item) { try { Query q = em.createQuery("SELECT i from Item i where i.id = :id"); q.setParameter("id", item.getId()); return q.getSingleResult(); } catch (PersistenceException e) {

catchEvent.fire(new ExceptionToCatchEvent(e)); } }}

TheEventofgenerictypeExceptionToCatchEventisinjectedintoyourclassforuselaterwithinatry/catchblock.

TheeventisfiredwithanewinstanceofExceptionToCatchEventconstructedwiththeexceptiontobehandled.

1

2

1

2

Page 25: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-ExceptionHandler

Exceptionsarehandledasynchronously.

@ExceptionHandler public class MyHandlers {

void printExceptions(@Handles ExceptionEvent<Throwable> evt) { System.out.println("Something bad happened:" + evt.getException().getMessage());

evt.handleAndContinue(); }}

Exceptionhandlermethodsareregisteredonbeansannotatedwith@ExceptionHandler

The@Handlesannotationonthefirstparameterdesignatesthismethodasanexceptionhandler.

Thishandlerdoesnotmodifytheinvocationofsubsequenthandlers,asdesignatedbyinvokinghandleAndContinue().

1

2

3

1

2

3

Page 26: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-Type-safeProjectStage

ThecurrentProjectStagecanbeinjected.

@Injectprivate ProjectStage projectStage;

//...

boolean isDevelopment = ProjectStage.Development.equals(this.projectStage);

YoucanalsousetheProjectStageatXHTMLfiles.

<h:panelGroup layout="block"rendered="#{applicationConfig.projectStage == 'Development'}" > <!-- HTML Snippet is shown only in Development stage --></h:panelGroup>

Page 27: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-Type-safeProjectStage

DeltaSpikecomeswiththefollowingpre-definedProjectStages:

1. UnitTest2. Development3. SystemTest4. IntegrationTest5. Staging6. Production

ButyoucancreateyourowncustomProjectStages.

Page 28: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-ProjectStageConfiguration

ItcanbesetusingDeltaSpikeConfigurationMechanism

-D org.apache.deltaspike.ProjectStage=Development

HowtoprovidetheseKey/ValuestoDeltaSpike?

1. Systemproperties2. Environmentproperties3. JNDIvalues-thebasenameis"java:comp/env/deltaspike/"4. Propertiesfilevalues-defaultfilenameis"META-INF/apache-

deltaspike.properties"

Page 29: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-DeltaSpikeConfigurationMechanismConfigurationAPI

String userName = ConfigResolver.getPropertyValue("user.name");

String dbUserName = ConfigResolver.getProjectStageAwarePropertyValue("db.username"); Integer dbPort = ConfigResolver

.resolve("db.port") .as(Integer.class) .withProjectStage(true) .withDefault(3306) .getValue();

Date deadline = ConfigResolver.resolve("project.deadline") .as(Date.class, new CustomDateConverter()).getValue());

Properties(Key/Value)

user.name = "Rafael"

db.username.Production = "Antoine"

db.username.Development = "Benevides"

db.port = 1234

project.deadline = 2017-04-01

12

3

4

12

23

4

Page 30: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-DeltaSpikeConfigurationMechanismInjectionofconfiguredvaluesintobeansusing@ConfigProperty@ApplicationScopedpublic class SomeRandomService{ @Inject @ConfigProperty(name = "endpoint.poll.interval") private Integer pollInterval;

@Inject @ConfigProperty(name = "endpoint.poll.servername") private String pollUrl;

... }

Page 31: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-Messagesandi18n

Type-safemessages-Beancreation

@Named("msg")@MessageBundlepublic interface MyMessages {

public String welcome();

public String welcomeTo(String username);

@MessageTemplate("{custom_message}") public String message();}

inthemessagebundle:welcometo=Welcometo%s

inthemessagebundle:custom_message=DeltaSpikeisawesome!

1

2

1

2

Page 32: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-Messagesandi18n

NowthemessagesbeanisreadytobeusedinJavaClasses

@Injectprivate MyMessages messages;//new FacesMessage(messages.welcomeTo("Rafael"));log.info(messages.message());

…oreveninsideJSP/JSFbecauseitusesa@Namedannotation.

<h1>#{msg.welcome}</h1>

Itusesthe“partialbean”moduletodynamicallycreateimplementationatruntime.

Page 33: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-@Exclude

It’[email protected]!

ExcludingaBeaninanyCase@Excludepublic class NoBean{ }

ExcludingaBeaninCaseofProjectStageDevelopment@Exclude(ifProjectStage = ProjectStage.Development.class)public class MyBean{ }

ExcludingaBeaniftheProjectStageisdifferentfromDevelopment@Exclude(exceptIfProjectStage = ProjectStage.Development.class)public class MyDevBean{ }

ExcludingaBeanbasedonanExpressionwhichEvaluatestoTrue@Exclude(onExpression = "db==prodDB")public class DevDbBean { }

Page 34: Apache Deltaspike the CDI Toolbox (Java One 2015)

Core-InjectingResources

DeltaSpikehassimpleAPIsforperformingbasicresourceloadingandpropertyfilereading.

@Inject@InjectableResource("myfile.properties")private InputStream is;

public String getVersion() throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { return br.readLine(); }}

TheInjectableResourceProviderinterfacecanbeimplementedtoallowreadingfromalternatesourcesifneeded.

Page 35: Apache Deltaspike the CDI Toolbox (Java One 2015)

DataModule

Page 36: Apache Deltaspike the CDI Toolbox (Java One 2015)

DataModule Datamoduleisanimplementationoftherepositorypattern.

AtthemomentitonlysupportRDBMSthruJPA.

Butitcouldbeextendedtosupportotherdataservices.

Page 37: Apache Deltaspike the CDI Toolbox (Java One 2015)

“ "ARepositoryrepresentsallobjectsofacertaintypeasaconceptualset.

Itactslikeacollection,exceptwithmoreelaboratequeryingcapability."DomainDrivenDesign

—EricEvans

Repositorypattern

Page 38: Apache Deltaspike the CDI Toolbox (Java One 2015)

DataModule-CreatingaRepository@Repositorypublic interface UserRepository extends EntityRepository<User, Long> {/* DeltaSpike creates a proxy which implements:

Long count();List<E> findAll();E findBy(PK);void flush();void refresh();void remove(E);E save(E);E saveAndFlush(E);

...and many others */}

Itusesthe“partialbean”moduletodynamicallycreateimplementationatruntime.

Page 39: Apache Deltaspike the CDI Toolbox (Java One 2015)

DataModule-Makingqueries@Repositorypublic interface UserRepository extends EntityRepository<User, Long> {

public User findByUsernameAndPassword(String username, char[] password);

@Query("SELECT u FROM User AS u WHERE u.role in (?1)") public List<Role> findByRoles(List<Role> roles);

}

Thenameofthemethodautomaticallycreatesthequery.Example:"SELECTuFROMUseruWHEREu.username=?1ANDu.password=?2"

Thequeryisdefinedinsidethe@Queryannotation.

1

2

1

2

Page 40: Apache Deltaspike the CDI Toolbox (Java One 2015)

DataModule-Pagination@Repositorypublic interface UserRepository extends EntityRepository<User, Long> {

@Query("select p from Person p where p.age between ?1 and ?2") QueryResult<Person> findAllByAge(int minAge, int maxAge);

}

QueryResult<Person> paged = personRepository.findByAge(age)

// Query API stylepaged.maxResults(10).firstResult(50).getResultList();

// or paging stylepaged.withPageSize(10).toPage(5).getResultList();

int totalPages = paged.countPages();

Page 41: Apache Deltaspike the CDI Toolbox (Java One 2015)

SecurityModule

Page 42: Apache Deltaspike the CDI Toolbox (Java One 2015)

SecurityModule-Simpleinterceptor-styleauthorization

Type-safeauthorization

@Retention(value = RetentionPolicy.RUNTIME)@Target({ ElementType.TYPE, ElementType.METHOD })@SecurityBindingTypepublic @interface AdminOnly {}

@ApplicationScopedpublic class SecuredBean {

@AdminOnly public void doSomething() { //... }}

Page 43: Apache Deltaspike the CDI Toolbox (Java One 2015)

SecurityModule-Simpleinterceptor-styleauthorization

Aninterceptor-styleclassisusedtodefinetheaccess

@ApplicationScopedpublic class ApplicationAuthorizer {

@Secures @AdminOnly public boolean verifyPermission(InvocationContext invocationContext, BeanManager manager, @Loggged User user) throws Exception { return user.getRole().equalsIgnoreCase("Admin"); }}

Page 44: Apache Deltaspike the CDI Toolbox (Java One 2015)

JSFModule

Page 45: Apache Deltaspike the CDI Toolbox (Java One 2015)

JSFModule-JSFMessages@MessageBundlepublic interface Messages {

@MessageTemplate("Welcome to DeltaSpike") String welcomeToDeltaSpike();

}

@Modelpublic class MyJSFBean {

@Inject private JsfMessage<Messages> messages;

//... messages.addInfo().welcomeToDeltaSpike();}

Page 46: Apache Deltaspike the CDI Toolbox (Java One 2015)

JSFModule-@WindowScoped "Thewindow-scopeislikeasessionperwindow"

@WindowScopedpublic class PreferencesBean implements Serializable { //...}

"Thereisn’talotofuse-caseswhichneedshareddatabetweenwindows"

Page 47: Apache Deltaspike the CDI Toolbox (Java One 2015)

JSFModule-Double-SubmitPrevention

"Toavoidthatthesamecontentofaformgetssubmittedandthereforeprocessedmultipletimes"

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ds="http://deltaspike.apache.org/jsf"> <h:head> <!-- head content --> </h:head> <h:body> <h:form> <!-- form content --> <ds:preventDoubleSubmit/> </h:form> </h:body></html>

Page 48: Apache Deltaspike the CDI Toolbox (Java One 2015)

SchedulerModule

Page 49: Apache Deltaspike the CDI Toolbox (Java One 2015)

SchedulerModule

ProvidesintegrationwithQuartz.

// Job will execute each minute@Scheduled(cronExpression = "0 0/1 * * * ?", onStartup = false)public class CdiAwareQuartzJob implements org.quartz.Job {

// And it can receive CDI injections @Inject private AdminServices service;

@Override public void execute(JobExecutionContext context) throws JobExecutionException { service.executeAdministrativeTask(); }}

@Injectprivate Scheduler<Job> jobScheduler;

//...jobScheduler.registerNewJob(CdiAwareQuartzJob.class);

Page 50: Apache Deltaspike the CDI Toolbox (Java One 2015)

OtherModules

Page 51: Apache Deltaspike the CDI Toolbox (Java One 2015)

OtherModules

Page 52: Apache Deltaspike the CDI Toolbox (Java One 2015)

WanttogofartheronCDI?TUT2376:AdvancedCDIinlivecoding

Tuesday27that8:30

CyrilMagninII/III

AntoninStefanutti&AntoineSD

Page 53: Apache Deltaspike the CDI Toolbox (Java One 2015)

Questions???

deltaspike.apache.org/