1 # Copyright 2007-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: java-osgi.eclass
9 # Java maintainers (java@gentoo.org)
10 # @BLURB: Java OSGi eclass
12 # This eclass provides functionality which is used by packages that need to be
13 # OSGi compliant. This means that the generated jars will have special headers
14 # in their manifests. Currently this is used only by Eclipse-3.3 - later we
15 # could extend this so that Gentoo Java system would be fully OSGi compliant.
19 # @ECLASS-VARIABLE: _OSGI_T
22 # We define _OSGI_T so that it does not contain a slash at the end.
23 # According to Paludis guys, there is currently a proposal for EAPIs that
24 # would require all variables to end with a slash.
27 # must get Diego to commit something like this to portability.eclass
29 if type -p realpath > /dev/null; then
31 elif type -p readlink > /dev/null; then
35 eerror "No readlink nor realpath found, cannot canonicalise"
39 # @FUNCTION: _java-osgi_plugin
40 # @USAGE: <plugin name>
43 # This is an internal function, not to be called directly.
46 # _java-osgi_plugin "JSch"
49 # @param $1 - bundle name
51 # We hardcode Gentoo as the vendor name
53 cat > "${_OSGI_T}/tmp_jar/plugin.properties" <<-EOF
59 # @FUNCTION: _java-osgi_makejar
60 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
63 # This is an internal function, not to be called directly.
66 # _java-osgi_makejar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
69 # @param $1 - name of jar to repackage with OSGi
70 # @param $2 - bundle symbolic name
71 # @param $3 - bundle name
72 # @param $4 - export-package header
73 _java-osgi_makejar() {
74 debug-print-function ${FUNCNAME} "$@"
76 (( ${#} < 4 )) && die "Four arguments are needed for _java-osgi_makejar()"
78 local absoluteJarPath="$(_canonicalise ${1})"
79 local jarName="$(basename ${1})"
81 mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
82 [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
84 cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
85 || die "Unable to uncompress correctly the original jar"
87 cat > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" <<-EOF
89 Bundle-ManifestVersion: 2
90 Bundle-Name: %bundleName
91 Bundle-Vendor: %vendorName
92 Bundle-Localization: plugin
93 Bundle-SymbolicName: ${2}
98 _java-osgi_plugin "${3}"
100 jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
101 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
102 rm -rf "${_OSGI_T}/tmp_jar"
105 # @FUNCTION: @java-osgi_dojar
106 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
108 # Rewrites a jar, and produce an OSGi compliant jar from arguments given on the command line.
109 # The arguments given correspond to the minimal set of headers
110 # that must be present on a Manifest file of an OSGi package.
111 # If you need more headers, you should use the *-fromfile functions below,
112 # that create the Manifest from a file.
113 # It will call java-pkg_dojar at the end.
116 # java-osgi_dojar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
119 # @param $1 - name of jar to repackage with OSGi
120 # @param $2 - bundle symbolic name
121 # @param $3 - bundle name
122 # @param $4 - export-package-header
124 debug-print-function ${FUNCNAME} "$@"
125 local jarName="$(basename ${1})"
126 _java-osgi_makejar "$@"
127 java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
130 # @FUNCTION: java-osgi_newjar
131 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
133 # Rewrites a jar, and produce an OSGi compliant jar.
134 # The arguments given correspond to the minimal set of headers
135 # that must be present on a Manifest file of an OSGi package.
136 # If you need more headers, you should use the *-fromfile functions below,
137 # that create the Manifest from a file.
138 # It will call java-pkg_newjar at the end.
141 # java-osgi_newjar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
144 # @param $1 - name of jar to repackage with OSGi
145 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
146 # @param $3 - bundle symbolic name
147 # @param $4 - bundle name
148 # @param $5 - export-package header
150 debug-print-function ${FUNCNAME} "$@"
151 local jarName="$(basename $1)"
153 if (( ${#} > 4 )); then
154 _java-osgi_makejar "${1}" "${3}" "${4}" "${5}"
155 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
157 _java-osgi_makejar "$@"
158 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
162 # @FUNCTION:_java-osgi_makejar-fromfile
163 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
166 # This is an internal function, not to be called directly.
169 # _java-osgi_makejar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "JSch" 1
172 # @param $1 - name of jar to repackage with OSGi
173 # @param $2 - path to the Manifest file
174 # @param $3 - bundle name
175 # @param $4 - automatic version rewriting (0 or 1)
176 _java-osgi_makejar-fromfile() {
177 debug-print-function ${FUNCNAME} "$@"
179 ((${#} < 4)) && die "Four arguments are needed for _java-osgi_makejar-fromfile()"
181 local absoluteJarPath="$(_canonicalise ${1})"
182 local jarName="$(basename ${1})"
184 mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
185 [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
187 cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
188 || die "Unable to uncompress correctly the original jar"
190 [[ -e "${2}" ]] || die "Manifest file ${2} not found"
192 # We automatically change the version if automatic version rewriting is on
195 cat "${2}" | sed "s/Bundle-Version:.*/Bundle-Version: ${PV}/" > \
196 "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
198 cat "${2}" > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
201 _java-osgi_plugin "${3}"
203 jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
204 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
205 rm -rf "${_OSGI_T}/tmp_jar"
208 # @FUNCTION: java-osgi_newjar-fromfile
209 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
211 # This function produces an OSGi compliant jar from a given manifest file.
212 # The Manifest Bundle-Version header will be replaced by the current version
213 # of the package, unless the --no-auto-version option is given.
214 # It will call java-pkg_newjar at the end.
217 # java-osgi_newjar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
221 # --no-auto-version - This option disables automatic rewriting of the
222 # version in the Manifest file
224 # @param $1 - name of jar to repackage with OSGi
225 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
226 # @param $3 - path to the Manifest file
227 # @param $4 - bundle name
228 java-osgi_newjar-fromfile() {
229 debug-print-function ${FUNCNAME} "$@"
230 local versionRewriting=1
232 if [[ "${1}" == "--no-auto-version" ]]; then
236 local jarName="$(basename ${1})"
238 if (( ${#} > 3 )); then
239 _java-osgi_makejar-fromfile "${1}" "${3}" "${4}" "${versionRewriting}"
240 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
242 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
243 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
247 # @FUNCTION: java-osgi_dojar-fromfile()
248 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name>
250 # This function produces an OSGi compliant jar from a given manifestfile.
251 # The Manifest Bundle-Version header will be replaced by the current version
252 # of the package, unless the --no-auto-version option is given.
253 # It will call java-pkg_dojar at the end.
256 # java-osgi_dojar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
260 # --no-auto-version - This option disables automatic rewriting of the
261 # version in the Manifest file
263 # @param $1 - name of jar to repackage with OSGi
264 # @param $2 - path to the Manifest file
265 # @param $3 - bundle name
266 java-osgi_dojar-fromfile() {
267 debug-print-function ${FUNCNAME} "$@"
268 local versionRewriting=1
270 if [[ "${1}" == "--no-auto-version" ]]; then
274 local jarName="$(basename ${1})"
276 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
277 java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"