001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2009 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id: DefaultInvocation.java 1102 2009-12-07 03:01:58Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.ri;
038    
039    import java.lang.reflect.Method;
040    import java.util.HashMap;
041    import java.util.Map;
042    import org.jomc.model.Instance;
043    import org.jomc.model.Modules;
044    import org.jomc.spi.Invocation;
045    
046    // SECTION-START[Documentation]
047    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
048    /**
049     * Default invocation.
050     * @see DefaultInvoker
051     *
052     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
053     * @version $Id: DefaultInvocation.java 1102 2009-12-07 03:01:58Z schulte2005 $
054     */
055    // </editor-fold>
056    // SECTION-END
057    // SECTION-START[Annotations]
058    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
059    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
060                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
061    // </editor-fold>
062    // SECTION-END
063    public class DefaultInvocation implements Invocation
064    {
065        // SECTION-START[DefaultInvocation]
066    
067        /** Constant for the context key of the {@code Object} of this invocation. */
068        public static final String OBJECT_KEY = Invocation.class.getName() + ".object";
069    
070        /** Constant for the context key of the {@code Method} of this invocation. */
071        public static final String METHOD_KEY = Invocation.class.getName() + ".method";
072    
073        /** Constant for the context key of the {@code Object[]} arguments of this invocation. */
074        public static final String ARGUMENTS_KEY = Invocation.class.getName() + ".arguments";
075    
076        /** Constant for the context key of the result {@code Object} of this invocation. */
077        public static final String RESULT_KEY = Invocation.class.getName() + ".result";
078    
079        /** Constant for the context key of the {@code Instance} corresponding to the object of this invocation. */
080        public static final String INSTANCE_KEY = Invocation.class.getName() + ".instance";
081    
082        /** Constant for the context key of the {@code Modules} corresponding to the object of this invocation. */
083        public static final String MODULES_KEY = Invocation.class.getName() + ".modules";
084    
085        /** Constant for the context key of the {@code ClassLoader} corresponding to the modules of this invocation. */
086        public static final String CLASSLOADER_KEY = Invocation.class.getName() + ".classLoader";
087    
088        /** The context of this invocation. */
089        private Map context;
090    
091        /**
092         * Creates a new {@code DefaultInvocation} instance taking an invocation to initialize the instance with.
093         *
094         * @param invocation The invocation to initialize the instance with.
095         */
096        public DefaultInvocation( final Invocation invocation )
097        {
098            this.context = new HashMap( invocation.getContext() );
099        }
100    
101        public Map getContext()
102        {
103            if ( this.context == null )
104            {
105                this.context = new HashMap();
106            }
107    
108            return this.context;
109        }
110    
111        public Object getObject()
112        {
113            return this.getContext().get( OBJECT_KEY );
114        }
115    
116        public Method getMethod()
117        {
118            return (Method) this.getContext().get( METHOD_KEY );
119        }
120    
121        public Object[] getArguments()
122        {
123            return (Object[]) this.getContext().get( ARGUMENTS_KEY );
124        }
125    
126        public Object getResult()
127        {
128            return this.getContext().get( RESULT_KEY );
129        }
130    
131        public void setResult( final Object value )
132        {
133            if ( value == null )
134            {
135                this.getContext().remove( RESULT_KEY );
136            }
137            else
138            {
139                this.getContext().put( RESULT_KEY, value );
140            }
141        }
142    
143        /**
144         * Gets the instance of the object of this invocation from the context of this invocation.
145         *
146         * @return The instance of the object of this invocation from the context of this invocation or {@code null}.
147         *
148         * @see #INSTANCE_KEY
149         */
150        public Instance getInstance()
151        {
152            return (Instance) this.getContext().get( INSTANCE_KEY );
153        }
154    
155        /**
156         * Gets the modules corresponding to the object of this invocation from the context of this invocation.
157         *
158         * @return The modules corresponding to the object of this invocation from the context of this invocation or
159         * {@code null}.
160         *
161         * @see #MODULES_KEY
162         */
163        public Modules getModules()
164        {
165            return (Modules) this.getContext().get( MODULES_KEY );
166        }
167    
168        /**
169         * Gets the class loader corresponding to the modules of this invocation from the context of this invocation.
170         *
171         * @return The class loader corresponding to the modules of this invocation from the context of this invocation or
172         * {@code null}.
173         *
174         * @see #CLASSLOADER_KEY
175         */
176        public ClassLoader getClassLoader()
177        {
178            return (ClassLoader) this.getContext().get( CLASSLOADER_KEY );
179        }
180    
181        // SECTION-END
182        // SECTION-START[Constructors]
183        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
184    
185        /** Creates a new {@code DefaultInvocation} instance. */
186        @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
187                                     comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-11/jomc-tools" )
188        public DefaultInvocation()
189        {
190            // SECTION-START[Default Constructor]
191            super();
192            // SECTION-END
193        }
194        // </editor-fold>
195        // SECTION-END
196        // SECTION-START[Dependencies]
197        // SECTION-END
198        // SECTION-START[Properties]
199        // SECTION-END
200        // SECTION-START[Messages]
201        // SECTION-END
202    }