1 # Copyright 2007-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: java-osgi.eclass
8 # Java maintainers (java@gentoo.org)
9 # @BLURB: Java OSGi eclass
11 # This eclass provides functionality which is used by packages that need to be
12 # OSGi compliant. This means that the generated jars will have special headers
13 # in their manifests. Currently this is used only by Eclipse-3.3 - later we
14 # could extend this so that Gentoo Java system would be fully OSGi compliant.
18 # @ECLASS-VARIABLE: _OSGI_T
21 # We define _OSGI_T so that it does not contain a slash at the end.
22 # According to Paludis guys, there is currently a proposal for EAPIs that
23 # would require all variables to end with a slash.
26 # must get Diego to commit something like this to portability.eclass
28 if type -p realpath > /dev/null; then
30 elif type -p readlink > /dev/null; then
34 eerror "No readlink nor realpath found, cannot canonicalise"
38 # @FUNCTION: _java-osgi_plugin
39 # @USAGE: <plugin name>
42 # This is an internal function, not to be called directly.
45 # _java-osgi_plugin "JSch"
48 # @param $1 - bundle name
50 # We hardcode Gentoo as the vendor name
52 cat > "${_OSGI_T}/tmp_jar/plugin.properties" <<-EOF
58 # @FUNCTION: _java-osgi_makejar
59 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
62 # This is an internal function, not to be called directly.
65 # _java-osgi_makejar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
68 # @param $1 - name of jar to repackage with OSGi
69 # @param $2 - bundle symbolic name
70 # @param $3 - bundle name
71 # @param $4 - export-package header
72 _java-osgi_makejar() {
73 debug-print-function ${FUNCNAME} "$@"
75 (( ${#} < 4 )) && die "Four arguments are needed for _java-osgi_makejar()"
77 local absoluteJarPath="$(_canonicalise ${1})"
78 local jarName="$(basename ${1})"
80 mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
81 [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
83 cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
84 || die "Unable to uncompress correctly the original jar"
86 cat > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" <<-EOF
88 Bundle-ManifestVersion: 2
89 Bundle-Name: %bundleName
90 Bundle-Vendor: %vendorName
91 Bundle-Localization: plugin
92 Bundle-SymbolicName: ${2}
97 _java-osgi_plugin "${3}"
99 jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
100 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
101 rm -rf "${_OSGI_T}/tmp_jar"
104 # @FUNCTION: @java-osgi_dojar
105 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
107 # Rewrites a jar, and produce an OSGi compliant jar from arguments given on the command line.
108 # The arguments given correspond to the minimal set of headers
109 # that must be present on a Manifest file of an OSGi package.
110 # If you need more headers, you should use the *-fromfile functions below,
111 # that create the Manifest from a file.
112 # It will call java-pkg_dojar at the end.
115 # java-osgi_dojar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
118 # @param $1 - name of jar to repackage with OSGi
119 # @param $2 - bundle symbolic name
120 # @param $3 - bundle name
121 # @param $4 - export-package-header
123 debug-print-function ${FUNCNAME} "$@"
124 local jarName="$(basename ${1})"
125 _java-osgi_makejar "$@"
126 java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
129 # @FUNCTION: java-osgi_newjar
130 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
132 # Rewrites a jar, and produce an OSGi compliant jar.
133 # The arguments given correspond to the minimal set of headers
134 # that must be present on a Manifest file of an OSGi package.
135 # If you need more headers, you should use the *-fromfile functions below,
136 # that create the Manifest from a file.
137 # It will call java-pkg_newjar at the end.
140 # java-osgi_newjar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
143 # @param $1 - name of jar to repackage with OSGi
144 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
145 # @param $3 - bundle symbolic name
146 # @param $4 - bundle name
147 # @param $5 - export-package header
149 debug-print-function ${FUNCNAME} "$@"
150 local jarName="$(basename $1)"
152 if (( ${#} > 4 )); then
153 _java-osgi_makejar "${1}" "${3}" "${4}" "${5}"
154 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
156 _java-osgi_makejar "$@"
157 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
161 # @FUNCTION:_java-osgi_makejar-fromfile
162 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
165 # This is an internal function, not to be called directly.
168 # _java-osgi_makejar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "JSch" 1
171 # @param $1 - name of jar to repackage with OSGi
172 # @param $2 - path to the Manifest file
173 # @param $3 - bundle name
174 # @param $4 - automatic version rewriting (0 or 1)
175 _java-osgi_makejar-fromfile() {
176 debug-print-function ${FUNCNAME} "$@"
178 ((${#} < 4)) && die "Four arguments are needed for _java-osgi_makejar-fromfile()"
180 local absoluteJarPath="$(_canonicalise ${1})"
181 local jarName="$(basename ${1})"
183 mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
184 [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
186 cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
187 || die "Unable to uncompress correctly the original jar"
189 [[ -e "${2}" ]] || die "Manifest file ${2} not found"
191 # We automatically change the version if automatic version rewriting is on
194 cat "${2}" | sed "s/Bundle-Version:.*/Bundle-Version: ${PV}/" > \
195 "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
197 cat "${2}" > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
200 _java-osgi_plugin "${3}"
202 jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
203 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
204 rm -rf "${_OSGI_T}/tmp_jar"
207 # @FUNCTION: java-osgi_newjar-fromfile
208 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
210 # This function produces an OSGi compliant jar from a given manifest file.
211 # The Manifest Bundle-Version header will be replaced by the current version
212 # of the package, unless the --no-auto-version option is given.
213 # It will call java-pkg_newjar at the end.
216 # java-osgi_newjar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
220 # --no-auto-version - This option disables automatic rewriting of the
221 # version in the Manifest file
223 # @param $1 - name of jar to repackage with OSGi
224 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
225 # @param $3 - path to the Manifest file
226 # @param $4 - bundle name
227 java-osgi_newjar-fromfile() {
228 debug-print-function ${FUNCNAME} "$@"
229 local versionRewriting=1
231 if [[ "${1}" == "--no-auto-version" ]]; then
235 local jarName="$(basename ${1})"
237 if (( ${#} > 3 )); then
238 _java-osgi_makejar-fromfile "${1}" "${3}" "${4}" "${versionRewriting}"
239 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
241 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
242 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
246 # @FUNCTION: java-osgi_dojar-fromfile
247 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name>
249 # This function produces an OSGi compliant jar from a given manifestfile.
250 # The Manifest Bundle-Version header will be replaced by the current version
251 # of the package, unless the --no-auto-version option is given.
252 # It will call java-pkg_dojar at the end.
255 # java-osgi_dojar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
259 # --no-auto-version - This option disables automatic rewriting of the
260 # version in the Manifest file
262 # @param $1 - name of jar to repackage with OSGi
263 # @param $2 - path to the Manifest file
264 # @param $3 - bundle name
265 java-osgi_dojar-fromfile() {
266 debug-print-function ${FUNCNAME} "$@"
267 local versionRewriting=1
269 if [[ "${1}" == "--no-auto-version" ]]; then
273 local jarName="$(basename ${1})"
275 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
276 java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"