001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.annotation; 014 015import static java.lang.annotation.ElementType.*; 016import static java.lang.annotation.RetentionPolicy.*; 017 018import java.lang.annotation.*; 019 020import jakarta.servlet.*; 021 022import org.apache.juneau.rest.*; 023 024/** 025 * Identifies a method that gets called during servlet initialization. 026 * 027 * <p> 028 * This method is called from within the {@link Servlet#init(ServletConfig)} method after the {@link org.apache.juneau.rest.RestContext.Builder} 029 * object has been created and initialized with the annotations defined on the class, but before the 030 * {@link RestContext} object has been created. 031 * 032 * <p> 033 * The only valid parameter type for this method is {@link org.apache.juneau.rest.RestContext.Builder} which can be used to configure the servlet. 034 * 035 * <p> 036 * An example of this is the <c>PetStoreResource</c> class that uses an init method to perform initialization 037 * of an internal data structure. 038 * 039 * <h5 class='figure'>Example:</h5> 040 * <p class='bjava'> 041 * <ja>@Rest</ja>(...) 042 * <jk>public class</jk> PetStoreResource <jk>extends</jk> BasicRestServlet <jk>implements</jk> BasicUniversalJenaConfig { 043 * 044 * <jc>// Our database.</jc> 045 * <jk>private</jk> Map<Integer,Pet> <jf>petDB</jf>; 046 * 047 * <ja>@RestInit</ja> 048 * <jk>public void</jk> onInit(RestContext.Builder <jv>builder</jv>) <jk>throws</jk> Exception { 049 * <jc>// Load our database from a local JSON file.</jc> 050 * <jf>petDB</jf> = JsonParser.<jsf>DEFAULT</jsf>.parse(getClass().getResourceAsStream(<js>"PetStore.json"</js>), LinkedHashMap.<jk>class</jk>, Integer.<jk>class</jk>, Pet.<jk>class</jk>); 051 * } 052 * } 053 * </p> 054 * 055 * <h5 class='section'>Notes:</h5><ul> 056 * <li class='note'> 057 * The method should return <jk>void</jk> although if it does return any value, the value will be ignored. 058 * <li class='note'> 059 * The method should be <jk>public</jk> although other visibilities are valid if the security manager allows it. 060 * <li class='note'> 061 * Static methods can be used. 062 * <li class='note'> 063 * Multiple init methods can be defined on a class. 064 * <br>Init methods on parent classes are invoked before init methods on child classes. 065 * <br>The order of init method invocations within a class is alphabetical, then by parameter count, then by parameter types. 066 * <li class='note'> 067 * The method can throw any exception causing initialization of the servlet to fail. 068 * <li class='note'> 069 * Note that if you override a parent method, you probably need to call <code><jk>super</jk>.parentMethod(...)</code>. 070 * <br>The method is still considered part of the parent class for ordering purposes even though it's 071 * overridden by the child class. 072 * </ul> 073 * 074 * <h5 class='section'>See Also:</h5><ul> 075 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.LifecycleHooks">Lifecycle Hooks</a> 076 * </ul> 077 */ 078@Target({METHOD,TYPE}) 079@Retention(RUNTIME) 080@Inherited 081@Repeatable(RestInitAnnotation.Array.class) 082public @interface RestInit { 083 084 /** 085 * Dynamically apply this annotation to the specified methods. 086 * 087 * <h5 class='section'>See Also:</h5><ul> 088 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a> 089 * </ul> 090 * 091 * @return The annotation value. 092 */ 093 String[] on() default {}; 094}