1 /* 2 * JBoss, Home of Professional Open Source 3 * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual 4 * contributors by the @authors tag. See the copyright.txt in the 5 * distribution for a full listing of individual contributors. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.jboss.as.quickstarts.cdi.decorator; 18 19 import java.util.logging.Logger; 20 21 import javax.decorator.Decorator; 22 import javax.decorator.Delegate; 23 import javax.inject.Inject; 24 25 /** 26 * CDI decorator class. 27 * 28 * The {@link Decorator} qualifier tells CDI this is a decorator of the {@link StaffService}. 29 * 30 * We inject {@link StaffService} and identify the delegate injection point of a decorator, using {@link Delegate} annotation. 31 * By default decorators are disabled. So this class will not be touched. To activate decorator, it must be specified in 32 * beans.xml descriptor. 33 * 34 * @author Ievgen Shulga 35 */ 36 @Decorator 37 public abstract class StaffServiceDecorator implements StaffService { 38 39 @Inject 40 @Delegate 41 private StaffService staffService; 42 private final Logger log = Logger.getLogger(StaffServiceDecorator.class.getName()); 43 44 /** 45 * We wrap {@link StaffServiceImpl#getStaff()} method of delegate object and decorate it with additional functionality. In 46 * this case after call to delegated method, we change {@link Staff} attributes and log information. 47 * 48 */ 49 @Override 50 public Staff getStaff() { 51 Staff staff = staffService.getStaff(); 52 staff.setBonus(200); 53 staff.setPosition("Team Lead"); 54 log.info("CDI decorator method was called!"); 55 return staff; 56 } 57 58 }