KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > deployers > helpers > AttachmentLocator


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployers.plugins.deployers.helpers;
23
24 import org.jboss.deployers.spi.deployer.DeploymentUnit;
25 import org.jboss.deployers.spi.structure.DeploymentContext;
26
27 /**
28  * Search a DeploymentUnit structure from child to parent for a matching
29  * attachment.
30  *
31  * @author Scott.Stark@jboss.org
32  * @version $Revision:$
33  */

34 public class AttachmentLocator
35 {
36    /**
37     * Get a named attachment
38     *
39     * @param name the name of the attachment
40     * @return the attachment or null if not present
41     * @throws IllegalArgumentException for a null name
42     */

43    static public Object JavaDoc search(DeploymentUnit unit, String JavaDoc name)
44    {
45       Object JavaDoc attachment = null;
46       DeploymentContext ctx = unit.getDeploymentContext();
47       while( attachment == null && ctx != null )
48       {
49          attachment = ctx.getDeploymentUnit().getAttachment(name);
50          ctx = ctx.getParent();
51       }
52       return attachment;
53    }
54
55    /**
56     * Get named attachment of a given type
57     *
58     * @param <T> the expected type
59     * @param name the name of the attachment
60     * @param expectedType the expected type
61     * @return the attachment or null if not present
62     * @throws IllegalArgumentException for a null name or expectedType
63     */

64    static public <T> T search(DeploymentUnit unit, String JavaDoc name, Class JavaDoc<T> expectedType)
65    {
66       Object JavaDoc result = search(unit, name);
67       if (result == null)
68          return null;
69       return expectedType.cast(result);
70    }
71
72    /**
73     * Get an attachment of the given type
74     *
75     * @param <T> the expected type
76     * @param type the type
77     * @return the attachment or null if not present
78     * @throws IllegalArgumentException for a null name or type
79     */

80    static public <T> T search(DeploymentUnit unit, Class JavaDoc<T> type)
81    {
82       return search(unit, type.getName(), type);
83    }
84 }
85
Popular Tags