KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > OSGiCompatibilityPolicy


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import org.ungoverned.moduleloader.search.CompatibilityPolicy;
39
40 public class OSGiCompatibilityPolicy implements CompatibilityPolicy
41 {
42     private Oscar m_oscar = null;
43
44     public OSGiCompatibilityPolicy(Oscar oscar)
45     {
46         m_oscar = oscar;
47     }
48
49     /**
50      * Compares two versioned identifiers.
51      * @param leftId the identifier to test for compatibility.
52      * @param leftVersion the version number to test for compatibility.
53      * @param rightId the identifier used as the compatibility base line.
54      * @param rightVersion the version used as the compatibility base line.
55      * @return <tt>0</tt> if the identifiers are equal, <tt>-1</tt> if the
56      * left identifier is less then the right identifier, and <tt>1</tt>
57      * if the left identifier is greater than the right identifier.
58      * @throws java.lang.IllegalArgumentException if the two identifiers
59      * are not comparable, i.e., they refer to completely different
60      * entities.
61     **/

62     public int compare(
63         Object JavaDoc leftId, Object JavaDoc leftVersion,
64         Object JavaDoc rightId, Object JavaDoc rightVersion)
65     {
66         if (isComparable(leftId, rightId))
67         {
68             return compareVersion((int[]) leftVersion, (int[]) rightVersion);
69         }
70         else
71         {
72             throw new IllegalArgumentException JavaDoc("Identifiers are not comparable.");
73         }
74     }
75
76     /**
77      * Returns whether the first import/export target is compatible
78      * with the second.
79      * @param leftId the identifier to test for compatibility.
80      * @param leftVersion the version number to test for compatibility.
81      * @param rightId the identifier used as the compatibility base line.
82      * @param rightVersion the version used as the compatibility base line.
83      * @return <tt>true</tt> if the left version number object is compatible
84      * with the right version number object, otherwise <tt>false</tt>.
85     **/

86     public boolean isCompatible(
87         Object JavaDoc leftId, Object JavaDoc leftVersion,
88         Object JavaDoc rightId, Object JavaDoc rightVersion)
89     {
90         try {
91             // The OSGi spec says that identifiers are always backwards compatible.
92
return (compare(leftId, leftVersion, rightId, rightVersion) >= 0);
93         } catch (Exception JavaDoc ex) {
94             return false;
95         }
96     }
97
98     /**
99      * Returns whether the two identifiers are comparable. This method was
100      * intended to allow different policies of comparing imports to exports,
101      * such as allowing "wildcard" exports, but it does not fully work for
102      * that purpose and is possibly not necessary.
103      *
104      * @param leftId the first identifier to test.
105      * @param rightId the second identifier to test.
106      * @return <tt>true</tt> if the two identifiers are comparable,
107      * <tt>false</tt> otherwise.
108     **/

109     private boolean isComparable(Object JavaDoc leftId, Object JavaDoc rightId)
110     {
111         return leftId.equals(rightId);
112     }
113
114     private int compareVersion(int[] left, int[] right)
115     {
116         if (left[0] > right[0])
117             return 1;
118         else if (left[0] < right[0])
119             return -1;
120         else if (left[1] > right[1])
121             return 1;
122         else if (left[1] < right[1])
123             return -1;
124         else if (left[2] > right[2])
125             return 1;
126         else if (left[2] < right[2])
127             return -1;
128         return 0;
129     }
130 }
Popular Tags