Project

General

Profile

getcp.sh

Roger Borrello, 06/16/2026 07:20 PM

Download (4.92 KB)

 
1
#!/bin/bash
2

    
3
#set -x
4

    
5
# Function to help us shorten classpath by finding relative path to jar file
6
# If it backs up to the root ('/') bail and return the absolute path.
7
relpath()
8
{
9
   local s d b
10
   s=$(cd ${1%%/};pwd); d=$(cd $2;pwd); b=;
11
   while [ "${d#$s/}" == "${d}" ] && [ "$s" != "/" ]
12
      do s=$(dirname $s); b="../${b}"; done;
13
   if [ "$s" == "/" ]; then echo $2; else echo ${b}${d#$s/}; fi
14
}
15

    
16
# defaults
17
cfgdir=$(pwd)
18
fwdlib="p2j.jar"
19
appname="hotel"
20

    
21
# First parameter is appname, if passed. Otherwise the default above
22
if [ ".$1." != ".." ]; then
23
   appname="$1"
24
fi
25
appjar="${appname}.jar"
26

    
27
# Second parameter is the path to the config, if passed. Otherwise the default above
28
# It is where the script that called this helper resides.
29
if [ ".$2." != ".." ]; then
30
   cfgdir="$(cd "$(dirname "$2")"; pwd)/$(basename "$2")"
31
fi
32

    
33
# Optional AppCDS (client) mode: when invoked with --appcds[=<type>] we use the
34
# curated classpath published by deploy_appcds.sh next to the <type>.jsa archive
35
# (see appcds/client/<type>.cpath). That file holds the exact classpath the
36
# archive was dumped against, so returning it verbatim keeps the client runtime
37
# classpath identical to the archive's. Only the client (ClientDriver) passes
38
# --appcds; the server (FwdLauncher) does not, so it keeps using the full FWD
39
# library set located by the rules below. If no curated file is found we fall
40
# through to those same rules.
41
appcds_mode=
42
appcds_type="default"
43
for arg in "$@"; do
44
   case "$arg" in
45
      --appcds)   appcds_mode=true ;;
46
      --appcds=*) appcds_mode=true; appcds_type="${arg#--appcds=}" ;;
47
   esac
48
done
49
if [ -n "$appcds_mode" ]; then
50
   for cand in "/opt/${appname}/appcds/client/${appcds_type}.cpath" \
51
               "${cfgdir}/../appcds/client/${appcds_type}.cpath"; do
52
      if [ -f "$cand" ]; then
53
         appcds_cpath=$(cat "$cand")
54
         if [ -n "$appcds_cpath" ]; then
55
            # Terminate with a colon to match the non-AppCDS output below.
56
            echo "${appcds_cpath}:"
57
            exit 0
58
         fi
59
      fi
60
   done
61
fi
62

    
63
# Setup classpath. First get the FWD jars and libraries, then the application's.
64
# For FWD, use p2j.jar as the determining factor. Rules:
65
# 1) Look for FWD_LIB, honor build directory
66
# 2) One directories above the cfgdir and in the lib directory from there ($cfgdir/../lib)
67
# 3) Two directories above our current directory, and in the p2j/lib directory from there (../../p2j/lib)
68
# 4) Two directories above our current directory, and in the p2j/build/lib directory from there (../../p2j/build/lib)
69
# 5) A well-known location
70
# Use the canonical form.
71
if [ -n "$FWD_LIB" ]; then
72
   #echo "Rule 1" > /tmp/x.x
73
   use_fwd_lib=true
74
   if [ -f "${FWD_LIB}/build/lib/${fwdlib}" ]; then
75
      full_fcpath=${FWD_LIB}/build/lib
76
   else
77
      full_fcpath=${FWD_LIB}/lib
78
   fi
79
elif [ -f "${cfgdir}/../lib/${fwdlib}" ]; then
80
   #echo "Rule 2" >> /tmp/x.x
81
   full_fcpath=$(cd "${cfgdir}/../lib"; pwd)
82
elif [ -f "../../p2j/lib/${fwdlib}" ]; then
83
   #echo "Rule 3" >> /tmp/x.x
84
   full_fcpath=$(cd "../../p2j/lib"; pwd)
85
elif [ -f "../../p2j/build/lib/${fwdlib}" ]; then
86
   #echo "Rule 4" >> /tmp/x.x
87
   full_fcpath=$(cd "../../p2j/build/lib"; pwd)
88
elif [ -f "/opt/fwd/lib/${fwdlib}" ]; then
89
   #echo "Rule 5" >> /tmp/x.x
90
   full_fcpath="/opt/fwd/lib"
91
else
92
   echo "Unable to locate FWD libraries. Is FWD_LIB set? Exiting."
93
   exit 1
94
fi
95
#echo "full_fcpath=$full_fcpath" >> /tmp/x.x
96

    
97
# Find jars, and attempt to use relative paths so as to keep it as short as possible unless FWD_LIB is specified.
98
[ -z "$use_fwd_lib" ] && fwdjars=$(relpath $(pwd) $full_fcpath) || fwdjars=$full_fcpath
99

    
100
#echo "Adding FWD jars in" $fwdjars
101
for each in $(find $fwdjars/ -name "*.jar"|sort);
102
do
103
   if [ -z $cpath ]; then
104
      cpath=$each
105
   else
106
      cpath=$cpath:$each
107
   fi
108
done
109

    
110
# Now look for application libraries.
111
# Get a relative path, so as to keep the classpath as short as possible
112
# Start with one directory up, then ../lib, then ../../lib or the cfgpath
113
if [ -d "../lib" ]; then
114
   full_cpath=$(cd "../lib"; pwd)
115
elif [ -d "../../lib" ]; then
116
   full_cpath=$(cd "../../lib"; pwd)
117
else
118
   full_cpath=${cfgdir}
119
fi
120
if [ -f ${full_cpath}/${appjar} ]; then
121
   full_cpath=$full_cpath
122
elif [ -f "${cfgdir}/../lib/${appjar}" ]; then
123
   full_cpath=$(cd "${cfgdir}/../lib"; pwd)
124
elif [ -f "${cfgdir}/../../lib/${appjar}" ]; then
125
   full_cpath=$(cd "${cfgdir}/../../lib"; pwd)
126
elif [ -f "/opt/${appname}/lib/${appjar}" ]; then
127
   full_cpath="/opt/${appname}/lib"
128
else
129
   echo "Unable to locate $appname libraries. Exiting."
130
   exit 1
131
fi
132
#echo "full_cpath=$full_cpath" >> /tmp/x.x
133
rel_path=$(relpath $(pwd) $full_cpath)
134
#echo "rel_path=$rel_path" >> /tmp/x.x
135

    
136
if [ "$fwdjars" != "$rel_path" ]; then
137
   #echo "Adding application jars in $rel_path since fwdjars=$fwdjars" >> /tmp/x.x
138
   for each in $(find $rel_path/ -name "*.jar");
139
   do
140
      cpath=$each:$cpath
141
   done
142
#else
143
#   echo "Didn't add application jars in $rel_path since fwdjars=$fwdjars" >> /tmp/x.x
144
fi
145
# Terminate with a colon
146
cpath=${cpath}:
147

    
148
echo $cpath
149