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