Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @javax.servlet.sip.annotation.SipServlet
- public class HelloSipWorld extends SipServlet {
- /**
- *
- */
- private static final long serialVersionUID = -9090594133332625693L;
- private static Log logger = LogFactory.getLog(HelloSipWorld.class);
- private static String newAppNameFromCallServiceString = "";
- static {
- System.out.println("INIT static HelloSipWorld. Vamos a escanear las anotaciones que tiene el fichero com.kurento.kmf.sip.package-info.java ");
- System.out.println("El proposito es poder coger el atributo name de la anotacion @CallService y aplicarselo al name de la anotacion @SipApplication.");
- try {
- Annotation[] listaDeclaredAnnotations = HelloSipWorld.class.getPackage().getDeclaredAnnotations();
- for (Annotation a:listaDeclaredAnnotations) {
- System.out.println("ANOTACIONES DECLARADAS: " + a.toString());
- }
- //las anotaciones que hay en el mismo paquete donde esta alojado HelloSipWorld
- //annotations located in the same package than HelloSipWorld
- Annotation[] arrayAnotations = HelloSipWorld.class.getPackage().getAnnotations();
- for(Annotation a:arrayAnotations) {
- System.out.println("ANOTACIONES: " + a.toString());
- }
- Class<?> pClass = Class.forName("com.kurento.kmf.sip.package-info");
- System.out.println("pClass: " + pClass.toString());
- Field field = Class.class.getDeclaredField("annotations");
- field.setAccessible(true);
- //este es el mapa de anotaciones que tiene una determinada clase, en este caso, la clase package-info.java
- Map<Class<? extends Annotation>, Annotation> annotationsMap = (Map<Class<? extends Annotation>, Annotation>) field.get(pClass);
- System.out.println("LISTA DE ANOTACIONES: " + annotationsMap.toString());
- for (Annotation anotattion : arrayAnotations) {
- if (anotattion instanceof CallService) {
- System.out.println("Procesando anotacion CallService...");
- final CallService oldCallService = (CallService) HelloSipWorld.class.getPackage().getAnnotation(CallService.class);
- System.out.println("Por defecto la anotacion CallService tiene como name = " + oldCallService.name());
- //asigno a la variable temporal el name de la etiqueta CallService para poder asignarselo luego cuando procese la anotacion SipApplication
- newAppNameFromCallServiceString = oldCallService.name();
- System.out.println("Asignado nombre de la etiqueta CallService a la variable temporal. newAppNameFromCallServiceString: "
- + newAppNameFromCallServiceString);
- System.out.println("Se acabó de procesar la anotacion CallService...");
- }
- if (anotattion instanceof SipApplication) {
- System.out.println("Procesando anotacion SipApplication...");
- final SipApplication oldSipApplicationAnotation = (SipApplication) annotationsMap.get(SipApplication.class);
- System.out.println("valor de oldSipApplicationAnotation: " + oldSipApplicationAnotation + " name: " + oldSipApplicationAnotation.name()); ;
- Annotation newSipApplicationAnnotation = new SipApplication() {
- @Override
- public Class<? extends Annotation> annotationType() {
- return oldSipApplicationAnotation.annotationType();
- }
- @Override
- public String description() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.description();
- }
- @Override
- public String displayName() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.displayName();
- }
- @Override
- public boolean distributable() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.distributable();
- }
- @Override
- public String largeIcon() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.largeIcon();
- }
- @Override
- public String smallIcon() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.smallIcon();
- }
- @Override
- public String mainServlet() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.mainServlet();
- }
- @Override
- public String name() {
- return newAppNameFromCallServiceString;
- }
- @Override
- public int proxyTimeout() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.proxyTimeout();
- }
- @Override
- public int sessionTimeout() {
- // TODO Auto-generated method stub
- return oldSipApplicationAnotation.sessionTimeout();
- }
- };
- //modificamos el mapa de las anotaciones que tiene anotadas la clase que estoy escaneando. en este caso, package-info.java
- annotationsMap.put(SipApplication.class, newSipApplicationAnnotation);
- //asi no!
- //final SipApplication modifiedSipApplicationAnotation = (SipApplication) HelloSipWorld.class.getAnnotation(SipApplication.class);
- final SipApplication modifiedSipApplicationAnotation = (SipApplication) annotationsMap.get(SipApplication.class);
- //esto en teoria provoca el cambio dentro del mapa annotationsMap, pq modifiedSipApplicationAnotation esta dentro del mapa y al llamar a name() le obligo a que coja el nuevo nombre!
- System.out.println("nombre de modifiedSipApplicationAnotation: " + modifiedSipApplicationAnotation.name());
- System.out.println("Se acabó procesar la anotacion SipApplication...");
- }
- }
- } catch (Exception xcp) {
- xcp.printStackTrace();
- }
- System.out.println("END static HelloSipWorld...");
- }
- ....
- //rest of methods of HelloSipWorld
- }//end of HelloSipWorld
- package-info.java file
- /**
- *
- * Provides the way to instantiated a class that extends SipServlet without using a sip.xml
- *
- * @author Alonso Isidoro Roman (aroman@naevatec.com)
- * @version 1.0.0
- */
- //La idea es poder coger el name de esta anotacion CallService y asignarsela a piñon al name de la anotacion SipApplication
- @CallService(name ="My first kurento app")
- @SipApplication(name = "default-name-sip-app", description = "default description of the SipApplication", displayName = "default-displayName")
- package com.kurento.kmf.sip;
- import javax.servlet.sip.annotation.SipApplication;
- import com.kurento.kmf.sip.annotation.CallService;
- //Important, this file is on the same package that HelloSipWorld.java
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement