java-osgi.eclass 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Copyright 2007-2015 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: java-osgi.eclass
  4. # @MAINTAINER:
  5. # java@gentoo.org
  6. # @AUTHOR:
  7. # Java maintainers (java@gentoo.org)
  8. # @BLURB: Java OSGi eclass
  9. # @DESCRIPTION:
  10. # This eclass provides functionality which is used by packages that need to be
  11. # OSGi compliant. This means that the generated jars will have special headers
  12. # in their manifests. Currently this is used only by Eclipse-3.3 - later we
  13. # could extend this so that Gentoo Java system would be fully OSGi compliant.
  14. inherit java-utils-2
  15. # @ECLASS-VARIABLE: _OSGI_T
  16. # @INTERNAL
  17. # @DESCRIPTION:
  18. # We define _OSGI_T so that it does not contain a slash at the end.
  19. # According to Paludis guys, there is currently a proposal for EAPIs that
  20. # would require all variables to end with a slash.
  21. _OSGI_T="${T/%\//}"
  22. # must get Diego to commit something like this to portability.eclass
  23. _canonicalise() {
  24. if type -p realpath > /dev/null; then
  25. realpath "${@}"
  26. elif type -p readlink > /dev/null; then
  27. readlink -f "${@}"
  28. else
  29. # can't die, subshell
  30. eerror "No readlink nor realpath found, cannot canonicalise"
  31. fi
  32. }
  33. # @FUNCTION: _java-osgi_plugin
  34. # @USAGE: <plugin name>
  35. # @INTERNAL
  36. # @DESCRIPTION:
  37. # This is an internal function, not to be called directly.
  38. #
  39. # @CODE
  40. # _java-osgi_plugin "JSch"
  41. # @CODE
  42. #
  43. # @param $1 - bundle name
  44. _java-osgi_plugin() {
  45. # We hardcode Gentoo as the vendor name
  46. cat > "${_OSGI_T}/tmp_jar/plugin.properties" <<-EOF
  47. bundleName="${1}"
  48. vendorName="Gentoo"
  49. EOF
  50. }
  51. # @FUNCTION: _java-osgi_makejar
  52. # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
  53. # @INTERNAL
  54. # @DESCRIPTION:
  55. # This is an internal function, not to be called directly.
  56. #
  57. # @CODE
  58. # _java-osgi_makejar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
  59. # @CODE
  60. #
  61. # @param $1 - name of jar to repackage with OSGi
  62. # @param $2 - bundle symbolic name
  63. # @param $3 - bundle name
  64. # @param $4 - export-package header
  65. _java-osgi_makejar() {
  66. debug-print-function ${FUNCNAME} "$@"
  67. (( ${#} < 4 )) && die "Four arguments are needed for _java-osgi_makejar()"
  68. local absoluteJarPath="$(_canonicalise ${1})"
  69. local jarName="$(basename ${1})"
  70. mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
  71. [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
  72. cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
  73. || die "Unable to uncompress correctly the original jar"
  74. cat > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" <<-EOF
  75. Manifest-Version: 1.0
  76. Bundle-ManifestVersion: 2
  77. Bundle-Name: %bundleName
  78. Bundle-Vendor: %vendorName
  79. Bundle-Localization: plugin
  80. Bundle-SymbolicName: ${2}
  81. Bundle-Version: ${PV}
  82. Export-Package: ${4}
  83. EOF
  84. _java-osgi_plugin "${3}"
  85. jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
  86. -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
  87. rm -rf "${_OSGI_T}/tmp_jar"
  88. }
  89. # @FUNCTION: @java-osgi_dojar
  90. # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
  91. # @DESCRIPTION:
  92. # Rewrites a jar, and produce an OSGi compliant jar from arguments given on the command line.
  93. # The arguments given correspond to the minimal set of headers
  94. # that must be present on a Manifest file of an OSGi package.
  95. # If you need more headers, you should use the *-fromfile functions below,
  96. # that create the Manifest from a file.
  97. # It will call java-pkg_dojar at the end.
  98. #
  99. # @CODE
  100. # java-osgi_dojar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
  101. # @CODE
  102. #
  103. # @param $1 - name of jar to repackage with OSGi
  104. # @param $2 - bundle symbolic name
  105. # @param $3 - bundle name
  106. # @param $4 - export-package-header
  107. java-osgi_dojar() {
  108. debug-print-function ${FUNCNAME} "$@"
  109. local jarName="$(basename ${1})"
  110. _java-osgi_makejar "$@"
  111. java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
  112. }
  113. # @FUNCTION: java-osgi_newjar
  114. # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
  115. # @DESCRIPTION:
  116. # Rewrites a jar, and produce an OSGi compliant jar.
  117. # The arguments given correspond to the minimal set of headers
  118. # that must be present on a Manifest file of an OSGi package.
  119. # If you need more headers, you should use the *-fromfile functions below,
  120. # that create the Manifest from a file.
  121. # It will call java-pkg_newjar at the end.
  122. #
  123. # @CODE
  124. # java-osgi_newjar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
  125. # @CODE
  126. #
  127. # @param $1 - name of jar to repackage with OSGi
  128. # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
  129. # @param $3 - bundle symbolic name
  130. # @param $4 - bundle name
  131. # @param $5 - export-package header
  132. java-osgi_newjar() {
  133. debug-print-function ${FUNCNAME} "$@"
  134. local jarName="$(basename $1)"
  135. if (( ${#} > 4 )); then
  136. _java-osgi_makejar "${1}" "${3}" "${4}" "${5}"
  137. java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
  138. else
  139. _java-osgi_makejar "$@"
  140. java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
  141. fi
  142. }
  143. # @FUNCTION:_java-osgi_makejar-fromfile
  144. # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
  145. # @INTERNAL
  146. # @DESCRIPTION:
  147. # This is an internal function, not to be called directly.
  148. #
  149. # @CODE
  150. # _java-osgi_makejar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "JSch" 1
  151. # @CODE
  152. #
  153. # @param $1 - name of jar to repackage with OSGi
  154. # @param $2 - path to the Manifest file
  155. # @param $3 - bundle name
  156. # @param $4 - automatic version rewriting (0 or 1)
  157. _java-osgi_makejar-fromfile() {
  158. debug-print-function ${FUNCNAME} "$@"
  159. ((${#} < 4)) && die "Four arguments are needed for _java-osgi_makejar-fromfile()"
  160. local absoluteJarPath="$(_canonicalise ${1})"
  161. local jarName="$(basename ${1})"
  162. mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
  163. [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
  164. cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
  165. || die "Unable to uncompress correctly the original jar"
  166. [[ -e "${2}" ]] || die "Manifest file ${2} not found"
  167. # We automatically change the version if automatic version rewriting is on
  168. if (( ${4} )); then
  169. cat "${2}" | sed "s/Bundle-Version:.*/Bundle-Version: ${PV}/" > \
  170. "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
  171. else
  172. cat "${2}" > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
  173. fi
  174. _java-osgi_plugin "${3}"
  175. jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
  176. -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
  177. rm -rf "${_OSGI_T}/tmp_jar"
  178. }
  179. # @FUNCTION: java-osgi_newjar-fromfile
  180. # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
  181. # @DESCRIPTION:
  182. # This function produces an OSGi compliant jar from a given manifest file.
  183. # The Manifest Bundle-Version header will be replaced by the current version
  184. # of the package, unless the --no-auto-version option is given.
  185. # It will call java-pkg_newjar at the end.
  186. #
  187. # @CODE
  188. # java-osgi_newjar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
  189. # @CODE
  190. #
  191. # @param $opt
  192. # --no-auto-version - This option disables automatic rewriting of the
  193. # version in the Manifest file
  194. #
  195. # @param $1 - name of jar to repackage with OSGi
  196. # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
  197. # @param $3 - path to the Manifest file
  198. # @param $4 - bundle name
  199. java-osgi_newjar-fromfile() {
  200. debug-print-function ${FUNCNAME} "$@"
  201. local versionRewriting=1
  202. if [[ "${1}" == "--no-auto-version" ]]; then
  203. versionRewriting=0
  204. shift
  205. fi
  206. local jarName="$(basename ${1})"
  207. if (( ${#} > 3 )); then
  208. _java-osgi_makejar-fromfile "${1}" "${3}" "${4}" "${versionRewriting}"
  209. java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
  210. else
  211. _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
  212. java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
  213. fi
  214. }
  215. # @FUNCTION: java-osgi_dojar-fromfile
  216. # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name>
  217. # @DESCRIPTION:
  218. # This function produces an OSGi compliant jar from a given manifestfile.
  219. # The Manifest Bundle-Version header will be replaced by the current version
  220. # of the package, unless the --no-auto-version option is given.
  221. # It will call java-pkg_dojar at the end.
  222. #
  223. # @CODE
  224. # java-osgi_dojar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
  225. # @CODE
  226. #
  227. # @param $opt
  228. # --no-auto-version - This option disables automatic rewriting of the
  229. # version in the Manifest file
  230. #
  231. # @param $1 - name of jar to repackage with OSGi
  232. # @param $2 - path to the Manifest file
  233. # @param $3 - bundle name
  234. java-osgi_dojar-fromfile() {
  235. debug-print-function ${FUNCNAME} "$@"
  236. local versionRewriting=1
  237. if [[ "${1}" == "--no-auto-version" ]]; then
  238. versionRewriting=0
  239. shift
  240. fi
  241. local jarName="$(basename ${1})"
  242. _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
  243. java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
  244. }