Java Web Services (1) - 第1章 Web服务快速入门
SCRIPTS_DIR=/Users/liuzhaofu/opus-dev/product/tools/dev
PRODUCT_DIR=/Users/liuzhaofu/opus-dev/product
NODE_MODULES_DIR=/Users/liuzhaofu/opus-dev/product/node/common
NODE_SERVICES_DIR=/Users/liuzhaofu/opus-dev/product/node/services
NODE_CONNECTORS_DIR=/Users/liuzhaofu/opus-dev/product/node/connectors
NODE_AUX_DIR=/Users/liuzhaofu/opus-dev/product/node/aux
WEB_UI_DIR=/Users/liuzhaofu/opus-dev/product/web/ui
OSSA_ASSETS_DIR=/Users/liuzhaofu/opus-dev/product/assets
OSSA_WORKING_DIR=/Users/liuzhaofu/ossa-work
/Users/liuzhaofu/ossa-work/logs
/Users/liuzhaofu/ossa-work/pkg
OSSA_APPS_DIR=/Users/liuzhaofu/ossa-work/apps
SCHEMA_FOLDER=/Users/liuzhaofu/opus-dev/product/languages/schemas/v0.1
API_PATH=/Users/liuzhaofu/opus-dev/product/node/services/common/api
1.1 Web服务杂项
1.2 Web服务有什么好处
1.3 Web服务和面向服务的架构
1.4 Web服务简史
1.4.1 从DCE/RPC到XML-RPC
1.4.2 分布式对象架构:Java示例
1.4.3 用Web服务来救援
1.5 什么是REST
1.5.1 动词和不透明名词
1.6 回顾HTTP请求和响应
HTTP响应头示例: (http://conkeyn.iteye.com/blog/687027)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cokkie: JSESSIONID=...; Path=/predictions/;
Content-Type: text/html;charset=ISO-8895-1
Transfer-Encoding: chunked
1.7 HTTP作为API
1.7.1 用Java编写的两个HTTP客户端
1.8 第一个REST式的例子
1.8.1 Predictions Web服务的工作原理
1.8.2 针对Predictions Web服务的客户端
1.9 为什么REST式Web服务要使用servlet
1.10 下一步是什么
#!/bin/bash #
# OSSA startup script
# Copyright(c) Oracle Corporation
# author: Hemant Raju <hemant.raju@oracle.com>
# ####################################################################
#Script to start OSSA in dev env on a single machine setup#
#
#################################################################### echo "#############################################################"
echo "OSSA startup script"
echo "#############################################################" #############################################################
#Initilization #
############################################################# log_info() {
echo "INFO: $1"
} log_warn() {
echo "WARN: $1"
} log_error() {
echo "ERRR: $1"
} init() {
OSSA_ENABLE_ELK=true currentDir=`pwd`
SCRIPTS_DIR=$currentDir #All env variables that have fixed values must be defined in ossa.env
#env variables that are calculated must be defined in this start script.
source $SCRIPTS_DIR/ossa.env
if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
#User specific settings
if [ -f "${HOME}/.ossa_profile" ]; then
log_info ".ossa_profile exists. Sourcing from .ossa_profile"
source ${HOME}/.ossa_profile
else
log_info ".ossa_profile does not exist. Creating one with defaults"
cp $SCRIPTS_DIR/.ossa_profile_template ${HOME}/.ossa_profile
source ${HOME}/.ossa_profile
fi
fi PRODUCT_DIR=${currentDir//\/tools\/dev}
log_info "Product dir - $PRODUCT_DIR" NODE_MODULES_DIR=$PRODUCT_DIR/node/common
log_info "Node modules dir - $NODE_MODULES_DIR" NODE_SERVICES_DIR=$PRODUCT_DIR/node/services
log_info "Node services dir - $NODE_SERVICES_DIR" NODE_CONNECTORS_DIR=$PRODUCT_DIR/node/connectors
log_info "Node connectors dir - $NODE_CONNECTORS_DIR" NODE_AUX_DIR=$PRODUCT_DIR/node/aux
log_info "Node Auxilliary dir - $NODE_AUX_DIR" WEB_UI_DIR=$PRODUCT_DIR/web/ui
log_info "Web UI dir - $WEB_UI_DIR" OSSA_ASSETS_DIR=$PRODUCT_DIR/assets SERVICE_PACKAGE_JSON_FILE=package.json
SERVICE_PORT_NUMBER=
SERVICE_HOST_NAME=localhost if [ -z "$OSSA_WORKING_DIR" ]; then
log_warn "OSSA_WORKING_DIR env variable not defined. Defaulting to <user.home>/ossa-work"
USER_HOME=${HOME}
OSSA_WORKING_DIR="$USER_HOME/ossa-work"
eval export OSSA_WORKING_DIR=$OSSA_WORKING_DIR
fi if [ ! -d "$OSSA_WORKING_DIR" ]; then
log_info "Creating OSSA working dir $OSSA_WORKING_DIR"
mkdir $OSSA_WORKING_DIR
mkdir $OSSA_WORKING_DIR/logs
fi if [ ! -d "$OSSA_WORKING_DIR/logs" ]; then
mkdir $OSSA_WORKING_DIR/logs
fi if [ ! -d "$OSSA_WORKING_DIR/pkg" ]; then
mkdir $OSSA_WORKING_DIR/pkg
fi
OSSA_PKG_DIR=$OSSA_WORKING_DIR/pkg if [ ! -d "$OSSA_WORKING_DIR/apps" ]; then
mkdir $OSSA_WORKING_DIR/apps
fi if [[ ! -z $NIMBULA_ENV && "$NIMBULA_ENV" == "true" ]]; then
log_info ".ossa_profile does not exist. Creating one with defaults under ${OSSA_WORKING_DIR}"
cp $SCRIPTS_DIR/.ossa_profile_template ${OSSA_WORKING_DIR}/.ossa_profile
source ${OSSA_WORKING_DIR}/.ossa_profile
fi OSSA_APPS_DIR=$OSSA_WORKING_DIR/apps export SCHEMA_FOLDER=$PRODUCT_DIR/languages/schemas/v0.
export API_PATH=$PRODUCT_DIR/node/services/common/api #if [ ! -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
# mkdir $USERCONTEXTSTORE_SAMPLE_DIR
#fi #if [ ! -d "$RECIPESTORE_SAMPLE_DIR" ]; then
# mkdir $RECIPESTORE_SAMPLE_DIR
#fi #if [ ! -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
# mkdir $RECIPECATALOGSTORE_SAMPLE_DIR
#fi OS=`uname -s`
if [[ "$OS" == "Linux" ]]; then
log_info "Running on $OS, adding /usr/sbin & /usr/local/packages/aime/ias to the path"
export PATH=$PATH:/usr/sbin:/usr/local/packages/aime/ias
if [[ -n $NODEJS_HOME ]]; then
export PATH=$PATH:$NODEJS_HOME/bin
fi
if [[ -n $MONGO_HOME ]]; then
export PATH=$PATH:$MONGO_HOME/bin
fi
fi if [ "$OS" == "Linux" ]; then
eval export REVERSEPROXY_HOST=`hostname`
fi if [ "$DB_TYPE" == "oracle" ]; then
FINAL_TRY_SLEEP_TIME=
else
FINAL_TRY_SLEEP_TIME=
fi log_info "OSSA working dir(logs, db, etc) - $OSSA_WORKING_DIR"
} delete_logs() {
log_info "Deleting $OSSA_WORKING_DIR/logs/-"
if [ -d "$OSSA_WORKING_DIR/logs" ]; then
rm -f $OSSA_WORKING_DIR/logs/-
fi
} cleanup() {
log_info "Stopping services..."
$SCRIPTS_DIR/stop delete_logs #log_info "Deleting $USERCONTEXTSTORE_SAMPLE_DIR"
#if [ -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
# rm -f $USERCONTEXTSTORE_SAMPLE_DIR/-
#fi #log_info "Deleting $RECIPESTORE_SAMPLE_DIR"
#if [ -d "$RECIPESTORE_SAMPLE_DIR" ]; then
# rm -rf $RECIPESTORE_SAMPLE_DIR/-
#fi #log_info "Deleting $RECIPECATALOGSTORE_SAMPLE_DIR"
#if [ -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
# rm -f $RECIPECATALOGSTORE_SAMPLE_DIR/-
#fi log_info "Stopping MongoDB. Deleting $OSSA_WORKING_DIR/mongodb"
pkill -f "mongod"
sleep
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_info "MongoDB stopped"
else
sleep
fi
if [ -d "$OSSA_WORKING_DIR/mongodb" ]; then
rm -rf $OSSA_WORKING_DIR/mongodb
fi if [ -d "$OSSA_WORKING_DIR/nginx" ]; then
rm -rf $OSSA_WORKING_DIR/nginx
fi if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
log_info "Stopping NGINX. Deleting $OSSA_WORKING_DIR/nginx" if [ "$OS" == "Linux" ]; then
if [ -d "/etc/nginx/ssl" ]; then
log_info "Deleting /etc/nginx/ssl folder"
run_as_root "rm -rf /etc/nginx/ssl"
fi
run_as_root "pkill nginx"
else
pkill nginx
fi
fi if [ "$OSSA_ENABLE_ELK" == "true" ]; then
$SCRIPTS_DIR/elk.sh -cleanup $OSSA_WORKING_DIR
fi } #############################################################
# Node Service functions #
#############################################################
get_service_name() {
local serviceFolder=$
local validateServiceName=$
unset SERVICE_NAME
unset SERVICE_VERSION
unset serviceName if [ ! -f "$serviceFolder/$SERVICE_PACKAGE_JSON_FILE" ]; then
log_error "Missing $SERVICE_PACKAGE_JSON_FILE file under $serviceFolder"
return
fi serviceName=`python $SCRIPTS_DIR/lib/getservicename.py package.json`
if [ "serviceName$serviceName" == "serviceName" ]; then
log_error "Service 'name' is not set in $serviceFolder/$SERVICE_PACKAGE_JSON_FILE"
return
fi if [ "$validateServiceName" == "true" ]; then
serviceNameValid=`echo "$serviceName" | tr -d '-' | tr -d ' '`
if [ "$serviceNameValid" != "$serviceName" ]; then
log_error "Service 'name' in $serviceFolder/$SERVICE_PACKAGE_JSON_FILE has invalid characters"
return
fi
fi SERVICE_NAME=`echo "$serviceName" | tr '[:lower:]' '[:upper:]'` return
} setup_service_env() {
local serviceFolder=$
if [ -z $ ]; then
local servicePath=$NODE_SERVICES_DIR/$serviceFolder
else
local servicePath=$/$serviceFolder
fi cd $servicePath
get_service_name $servicePath envHostVar=`env | grep ${SERVICE_NAME}_SERVICE_HOST`
envPortVar=`env | grep ${SERVICE_NAME}_SERVICE_PORT`
if [ "host$envHostVar" != "host" ]; then
if [ "port$envPortVar" != "port" ]; then
#log_info "Env variables ${SERVICE_NAME}_SERVICE_HOST and ${SERVICE_NAME}_SERVICE_PORT already defined"
#log_info $envHostVar
#log_info $envPortVar
return
fi
fi local lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"` while [ "portavailable$lsofRet" != "portavailable" ];
do
log_warn "Port $SERVICE_PORT_NUMBER not available. Trying next available port...."
SERVICE_PORT_NUMBER=`expr $SERVICE_PORT_NUMBER + `
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
done log_info "Port $SERVICE_PORT_NUMBER is available" eval export ${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER
eval export ${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME #log_info "${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER"
#log_info "${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME" #echo "export ${SERVICE_NAME}_SERVICE_PORT=$SERVICE_PORT_NUMBER" >> $SCRIPTS_DIR/ossa.env
#echo "export ${SERVICE_NAME}_SERVICE_HOST=$SERVICE_HOST_NAME" >> $SCRIPTS_DIR/ossa.env SERVICE_PORT_NUMBER=`expr $SERVICE_PORT_NUMBER + ` return
} separator() {
echo
echo "**********************************************************************************"
} start_node_service() { local serviceFolder=$
if [ -z $ ]; then
local servicePath=$NODE_SERVICES_DIR/$serviceFolder
else
local servicePath=$/$serviceFolder
fi if [ "$3" == "--no-validation" ]; then
local validateServiceName=false
local portCheck=false
else
local validateServiceName=true
local portCheck=true
fi separator
log_info "Service $serviceFolder - starting..." cd $servicePath
get_service_name $servicePath $validateServiceName if [ $? == ]; then
SERVICE_PORT_NUMBER=`env | grep ${SERVICE_NAME}_SERVICE_PORT | awk -F= '{ print $NF }'`
if [ "$portCheck" == "true" ]; then
local serviceUid=OSSA_SERVICE_${SERVICE_NAME}_$SERVICE_PORT_NUMBER
else
local serviceUid=OSSA_SERVICE_${SERVICE_NAME}
fi local ossaServiceProcess=`pgrep -f $serviceUid`
if [ "ossaServiceRunning$ossaServiceProcess" != "ossaServiceRunning" ]; then
log_info "$serviceUid already running. Skipping"
return
fi if [ -f $OSSA_WORKING_DIR/logs/$serviceName-stdout.log ]; then
rm -f $OSSA_WORKING_DIR/logs/$serviceName-stdout.log
fi
if [ -f $OSSA_WORKING_DIR/logs/$serviceName.log ]; then
rm -f $OSSA_WORKING_DIR/logs/$serviceName.log
fi if [[ -z $NPM_INSTALL || "$NPM_INSTALL" == "true" ]]; then
log_info "Service $serviceFolder - npm install/update..."
npm config set registry https://artifactory-slc.oraclecorp.com/artifactory/api/npm/npm-virtual
if [ -d $servicePath/node_modules ]; then
log_info "Service $serviceFolder - npm update..."
npm update --depth= >>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log >&
else
log_info "Service $serviceFolder - npm install..."
npm install >>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log >&
fi
fi
if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for k in `find node_modules -maxdepth -type d -name 'ossa*'`
do
dependency=$(basename $k)
log_info "Service $serviceFolder - linking to $dependency"
npm link $dependency
done
fi
log_info "Service $serviceFolder - npm start..."
npm start --uid $serviceUid >>$OSSA_WORKING_DIR/logs/$serviceName-stdout.log >& & sleep if [ "$portCheck" == "true" ]; then
sleep
local lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
sleep
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_warn "Service $SERVICE_NAME - waiting to bind to port $SERVICE_PORT_NUMBER"
sleep
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_warn "Service $SERVICE_NAME - still waiting to bind to port $SERVICE_PORT_NUMBER."
sleep $FINAL_TRY_SLEEP_TIME
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
if [ "$DB_TYPE" == "oracle" ]; then
log_warn "Service $SERVICE_NAME - possibly remote OracleDB connection, giving some more time..."
sleep $FINAL_TRY_SLEEP_TIME
fi
lsofRet=`lsof -i -P | grep ":$SERVICE_PORT_NUMBER"`
if [ "portavailable$lsofRet" == "portavailable" ]; then
log_error "Service $SERVICE_NAME - failed to bind to port $SERVICE_PORT_NUMBER"
log_error
if [ -f $OSSA_WORKING_DIR/logs/$serviceName.log ]; then
cat $OSSA_WORKING_DIR/logs/$serviceName.log | grep error
else
cat $OSSA_WORKING_DIR/logs/$serviceName-stdout.log | grep Error
fi exit ;
fi
fi
fi
fi
fi
fi local serviceProcess=`pgrep -f $serviceUid`
if [ "servicerunning$serviceProcess" == "servicerunning" ]; then
log_error "Service $SERVICE_NAME - failed to start"
exit ;
fi
if [ "$portCheck" == "true" ]; then
log_info "Service $SERVICE_NAME - started successfully on port $SERVICE_PORT_NUMBER"
else
log_info "Service $SERVICE_NAME - started successfully"
fi
else
log_error "Service $serviceFolder - failed to setup env"
log_error "Service $serviceFolder - NOT STARTED"
exit
fi
} #############################################################
# Seed OUDLs & ORELs
#############################################################
seed_oudls_orels() {
log_info "Seeding OUDLs and ORELs..."
local seedFile=$SCRIPTS_DIR/lib/ossa-seed-users.csv
local ossaUserOudl=$PRODUCT_DIR/languages/concrete/oudl/ossa.oudl
local orelFolder=$PRODUCT_DIR/languages/concrete/orel
local ossaOrelFolder=$PRODUCT_DIR/languages/concrete/orel/ossaUserRecipes if [ -d "$USERCONTEXTSTORE_SAMPLE_DIR" ]; then
rm -f $USERCONTEXTSTORE_SAMPLE_DIR/-
else
log_error "$USERCONTEXTSTORE_SAMPLE_DIR not found"
exit
fi if [ -d "$RECIPESTORE_SAMPLE_DIR" ]; then
rm -rf $RECIPESTORE_SAMPLE_DIR/-
else
log_error "$RECIPESTORE_SAMPLE_DIR not found"
exit
fi awk -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" 'BEGIN {
FS=",";
}
{
userId=$
gsub(" ","",userId);
gsub("@oracle.com","",userId); cmd="mkdir "seedOrelDir"/"userId;
system(cmd);
}
END { }
' $seedFile #Public recipes
mkdir $RECIPESTORE_SAMPLE_DIR/public
cp $orelFolder/-.orel $RECIPESTORE_SAMPLE_DIR/public for j in `find $ossaOrelFolder -name *.orel`
do
awk -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" -v ossaOrel="$j" 'BEGIN {
FS=",";
}
{
userName=$
userId=$
userFullId=$
gsub(" ","",userId);
gsub("@oracle.com","",userId);
gsub(" ","",userFullId); len=split(ossaOrel, a, "/");
ossaOrelFile=a[len]; cmd="cp "ossaOrel" "seedOrelDir"/"userId"/"ossaOrelFile".copy";
system(cmd); cmd="sed -e '\''s/\"id\": \"ossa./\"id\": \""userFullId"_/'\'' -e '\''s/\"name\": \"/\"name\": \""userName" /'\'' -e '\''s/\"userId\": \"ossa\"/\"userId\": \""userFullId"\"/'\'' "seedOrelDir"/"userId"/"ossaOrelFile".copy > "seedOrelDir"/"userId"/"ossaOrelFile;
system(cmd); cmd="rm "seedOrelDir"/"userId"/"ossaOrelFile".copy";
system(cmd);
}
END { }
' $seedFile
done awk -v seedOudlDir="$USERCONTEXTSTORE_SAMPLE_DIR" -v ossaUserOudl="$ossaUserOudl" 'BEGIN {
FS=",";
}
{
userName=$
userFileName=$
userFullId=$
gsub(" ","",userFileName);
gsub("@oracle.com","",userFileName);
gsub(" ","",userFullId); cmd="cp "ossaUserOudl" "seedOudlDir"/"userFileName;
system(cmd); cmd="sed -e '\''s/\"id\": \"ossa\"/\"id\": \""userFullId"\"/'\'' -e '\''s/\"name\": \"ossa-user\"/\"name\": \""userName"\"/'\'' "seedOudlDir"/"userFileName " > "seedOudlDir"/"userFileName".oudl";
system(cmd); cmd="rm "seedOudlDir"/"userFileName;
system(cmd);
}
END { }
' $seedFile log_info "Done seeding OUDLs and ORELs"
} #############################################################
# Seed OUDLs & ORELs via REST API calls
############################################################# init_seeding() {
export USERCONTEXTSTORE_CREATE_TABLE=true
export RECIPESTORE_CREATE_TABLE=true
export RECIPECATALOGSTORE_CREATE_TABLE=true
export CONNECTORSTORE_CREATE_TABLE=true
export OCDLSTORE_CREATE_TABLE=true
export SCHEDULERSTORE_CREATE_TABLE=true
export TIMER_CREATE_TABLE=true
} seed_oudls_orels_restapi() {
separator
log_info "Seeding Users, Recipes, Catalogs..." python $PRODUCT_DIR/tools/dev/lib/seeddb.py $PRODUCT_DIR if [ $? != ]; then
log_error "Seeding failed."
else
log_info "Seeding completed successfully."
fi
} #############################################################
# Seed Recipe Catalog
#############################################################
seed_recipe_catalog() {
log_info "Seeding recipe catalog..." local seedFile=$SCRIPTS_DIR/lib/ossa-seed-users.csv
local ossaMyRecipeCatalog=$PRODUCT_DIR/languages/concrete/catalog/ossaMyRecipes.json
local recipeCatalogDir=$PRODUCT_DIR/languages/concrete/catalog if [ -d "$RECIPECATALOGSTORE_SAMPLE_DIR" ]; then
rm -f $RECIPECATALOGSTORE_SAMPLE_DIR/-
else
log_error "$RECIPECATALOGSTORE_SAMPLE_DIR not found"
exit
fi recipeIds=`python $SCRIPTS_DIR/lib/getrecipeids.py $RECIPESTORE_SAMPLE_DIR/public`
sed -e s/@RECIPE_IDS@/$recipeIds/ $recipeCatalogDir/public.json > $RECIPECATALOGSTORE_SAMPLE_DIR/public.json awk -v seedRecipeCatalogDir="$RECIPECATALOGSTORE_SAMPLE_DIR" -v ossaMyRecipeCatalog="$ossaMyRecipeCatalog" -v scriptsDir="$SCRIPTS_DIR" -v seedOrelDir="$RECIPESTORE_SAMPLE_DIR" 'BEGIN {
FS=",";
}
{
seedOrelFolderName=$
userName=$
userFileName=$
userFullId=$ gsub(" ","",seedOrelFolderName);
gsub("@oracle.com","",seedOrelFolderName);
gsub(" ","",userFileName);
gsub(" ","",userFullId); seedMyRecipeFile=seedRecipeCatalogDir"/"userFileName"MyRecipes.json"
cmd="python "scriptsDir"/lib/getrecipeids.py "seedOrelDir"/"seedOrelFolderName
cmd | getline recipeIds
close(cmd) cmd="sed -e '\''s/ossa/"userFullId"/'\'' -e '\''s/OSSA/"userName"/'\'' -e '\''s/@RECIPE_IDS@/"recipeIds"/'\'' "ossaMyRecipeCatalog" > "seedMyRecipeFile;
cmd="sed -e '\''s/ossa/"userFullId"/'\'' -e '\''s/OSSA/"userName"/'\'' -e '\''s/@RECIPE_IDS@/"recipeIds"/'\'' "ossaMyRecipeCatalog" > "seedMyRecipeFile;
system(cmd);
}
END { }
' $seedFile log_info "Done seeding recipe catalog"
} #############################################################
# Backend Database MongoDB/Oracle NoSQL
#############################################################
start_mongodb() {
if [ ! -d "$OSSA_WORKING_DIR/mongodb" ]; then
mkdir $OSSA_WORKING_DIR/mongodb
fi if [ ! -d "$OSSA_WORKING_DIR/mongodb/db" ]; then
mkdir $OSSA_WORKING_DIR/mongodb/db
fi if [ ! -d "$OSSA_WORKING_DIR/mongodb/logs" ]; then
mkdir $OSSA_WORKING_DIR/mongodb/logs
fi
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_warn "MongoDB not running. Attempting to start MongoDB..."
if ! type "mongod" > /dev/null; then
log_error "mongod executable not found"
log_info "Install MongoDB. If already installed, make sure mongod executable is in PATH."
exit
fi
mongod --fork --dbpath $OSSA_WORKING_DIR/mongodb/db --logpath $OSSA_WORKING_DIR/mongodb/logs/mongodb.log
sleep
mongoDBProcess=`pgrep mongod`
if [ "mongodrunning$mongoDBProcess" == "mongodrunning" ]; then
log_error "Unable to start MongoDB."
exit ;
fi
log_info "MongoDB started successfully"
else
log_info "MongoDB already running"
fi
} #############################################################
# NGINX
#############################################################
start_nginx() { if [ "$OS" == "Linux" ]; then
if [ ! -f "/etc/nginx/ssl/nginx.crt" ]; then
if [ ! -d "/etc/nginx/ssl" ]; then
log_info "Creating /etc/nginx/ssl folder"
run_as_root "mkdir /etc/nginx/ssl"
fi
local serverName=`hostname`
local genCert="openssl req \
-new \
-newkey rsa: \
-days \
-nodes \
-x509 \
-subj \"/C=US/ST=CA/L=Redwood City/O=Oracle Corporation/CN=$serverName\" \
-keyout /etc/nginx/ssl/nginx.key \
-out /etc/nginx/ssl/nginx.crt"
echo $genCert
run_as_root "$genCert"
fi
fi if [ ! -d "$OSSA_WORKING_DIR/nginx/logs" ]; then
mkdir $OSSA_WORKING_DIR/nginx/logs
fi nginxProcess=`pgrep nginx`
if [ "nginxrunning$nginxProcess" == "nginxrunning" ]; then
log_warn "NGINX not running. Attempting to start NGINX..."
if ! type "nginx" > /dev/null; then
log_error "nginx executable not found"
log_info "Do 'brew install nginx' to install nginx. If already installed, make sure nginx is in PATH."
exit
fi local nginxStart="nginx -c $OSSA_WORKING_DIR/nginx/config/nginx.conf"
if [ "$OS" == "Linux" ]; then
run_as_root "$nginxStart"
else
$nginxStart
fi sleep
nginxProcess=`pgrep nginx`
if [ "nginxrunning$nginxProcess" == "nginxrunning" ]; then
log_error "Unable to start NGINX."
exit ;
fi
log_info "NGINX started successfully"
else
log_info "NGINX already running"
fi
} #############################################################
#
# Web UI
#
#############################################################
start_webui() {
if [ ! -d "$WEB_UI_DIR/public/js/libs/oj" ]; then
if [ ! -f "$OSSA_WORKING_DIR/pkg/oraclejet.zip" ]; then
log_info "Downloading JET..."
cd $OSSA_WORKING_DIR/pkg
curl -LOk http://jet.us.oracle.com/1.1.1/code/oraclejet.zip
fi
unzip $OSSA_WORKING_DIR/pkg/oraclejet.zip -d $WEB_UI_DIR/public
fi SERVICE_PORT_NUMBER=
setup_service_env ui $PRODUCT_DIR/web
start_node_service ui $PRODUCT_DIR/web
} #############################################################
#
#Initilize the dependent node modules
#
#############################################################
init_node_modules() {
separator if [ "$NPM_LINK" != "true" ]; then
log_info "Skipping ossa node modules initialization as NPM_LINK is not set to true"
return
fi log_info "Initializing dependent node modules..." if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for i in `find $NODE_MODULES_DIR -maxdepth -name package.json`
do
moduleFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'`
if [[ $moduleFolder != 'ossa-ocdl-validator' ]]; then
log_info "Module $moduleFolder - linking"
cd $NODE_MODULES_DIR/$moduleFolder
npm link
fi
done
fi for j in `find $NODE_MODULES_DIR -maxdepth -name package.json`
do
moduleFolder=`echo $j | awk '{
len=split($, a, "/")
print a[len-]
}'`
log_info "Module $moduleFolder - initializing"
cd $NODE_MODULES_DIR/$moduleFolder
if [[ -z $NPM_INSTALL || "$NPM_INSTALL" == "true" ]]; then
npm config set registry https://artifactory-slc.oraclecorp.com/artifactory/api/npm/npm-virtual
npm install
fi if [ $moduleFolder != 'ossa-ocdl-validator' ]; then
if [ "$NPM_LINK" == "true" ]; then
log_info "Doing npm linking"
for k in `find node_modules -maxdepth -type d -name 'ossa*'`
do
dependency=$(basename $k)
log_info "Module $moduleFolder - linking to $dependency"
npm link $dependency
done
fi
fi
done log_info "Done initializing dependent node modules"
} #############################################################
#
#Start the node.js microservices
#
#############################################################
start() { if [ "$DB_TYPE" != "oracle" ]; then
start_mongodb
fi #Before starting nginx, create the config files. Useful on nimbula instance machines, where nginx has to be started manually
if [ ! -d "$OSSA_WORKING_DIR/nginx" ]; then
mkdir $OSSA_WORKING_DIR/nginx
fi
if [ ! -d "$OSSA_WORKING_DIR/nginx/config" ]; then
mkdir $OSSA_WORKING_DIR/nginx/config
fi
if [ ! -f "$OSSA_WORKING_DIR/nginx/config/nginx.conf" ]; then
log_info "Creating nginx.conf file under $OSSA_WORKING_DIR/nginx/config"
if [ "$OS" == "Linux" ]; then
local serverName=`hostname`
sed -e s,'<%=@OSSA_WORKING_DIR%>',$OSSA_WORKING_DIR,g -e s,'<%=@SCRIPTS_DIR%>',$SCRIPTS_DIR,g -e s,'<%=@OSSA_ASSETS_DIR%>',$OSSA_ASSETS_DIR,g -e s,'<%=@SERVER_NAME%>',$serverName,g $SCRIPTS_DIR/nginx-linux.conf > $OSSA_WORKING_DIR/nginx/config/nginx.conf
else
sed -e s,@OSSA_WORKING_DIR@,$OSSA_WORKING_DIR,g -e s,@SCRIPTS_DIR@,$SCRIPTS_DIR,g -e s,@OSSA_ASSETS_DIR@,$OSSA_ASSETS_DIR,g $SCRIPTS_DIR/nginx.conf > $OSSA_WORKING_DIR/nginx/config/nginx.conf
fi
fi if [[ -z $NIMBULA_ENV || "$NIMBULA_ENV" == "false" ]]; then
start_nginx
fi if [ "$OSSA_LOGGER_LOGSTASH" == "true" ] && [ "$OSSA_ENABLE_ELK" == "true" ]; then
$SCRIPTS_DIR/elk.sh -start $PRODUCT_DIR $OSSA_WORKING_DIR $OSSA_PKG_DIR $OSSA_APPS_DIR
fi
start_webui
init_node_modules separator
log_info "Setting env variables for node services..." #Fix port number for API_GW as Web UI cannot read env variables.
SERVICE_PORT_NUMBER=
setup_service_env apigw SERVICE_PORT_NUMBER= #Main node services
for i in `find $NODE_SERVICES_DIR -maxdepth -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'` if [ $serviceFolder != 'opus-connector' ] &&
[ $serviceFolder != 'ossa-pod-manager' ] &&
[ $serviceFolder != 'test' ]; then
setup_service_env $serviceFolder
fi
done #Node Connectors dir
for i in `find $NODE_CONNECTORS_DIR -maxdepth -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'` setup_service_env $serviceFolder $NODE_CONNECTORS_DIR
done #Node Aux dir
for i in `find $NODE_AUX_DIR -maxdepth -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'` #if [ $serviceFolder != 'statsd-connector' ]; then
#setup_service_env $serviceFolder $NODE_AUX_DIR
#fi
done separator
log_info "Starting node services..." for i in `find $NODE_SERVICES_DIR -maxdepth -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'` if [ $serviceFolder != 'opus-connector' ] &&
[ $serviceFolder != 'ossa-pod-manager' ] &&
[ $serviceFolder != 'test' ]; then
start_node_service $serviceFolder
fi
done separator
log_info "Starting built-in connectors..." for i in `find $NODE_CONNECTORS_DIR -maxdepth -name app.js `
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'`
start_node_service $serviceFolder $NODE_CONNECTORS_DIR
done # separator
# log_info "Starting Auxilliary node services..."
#
# for i in `find $NODE_AUX_DIR -maxdepth -name app.js `
# do
# serviceFolder=`echo $i | awk '{
# len=split($, a, "/")
# print a[len-]
# }'`
#
#
# start_node_service $serviceFolder $NODE_AUX_DIR --no-validation
# done separator
log_info "All log files are under $OSSA_WORKING_DIR/logs"
echo
} ############################################################
# Get Service Folder from Service Name
############################################################
get_service_folder() {
local searchFolder=$
local searchServiceName=$ for i in `find $searchFolder -maxdepth -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
if [ "$pkgServiceName" == "$searchServiceName" ]; then
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'`
break
fi
done
} ############################################################
# Do an action based on arguments
############################################################
init
log_info "Done initialization."
separator
#log_info "Environment variables...."
#log_info
#env
#separator
if [ "$1" == "--reinstall" ]; then
cleanup for i in `find $NODE_SERVICES_DIR -maxdepth -name app.js`
do
serviceFolder=`echo $i | awk '{
len=split($, a, "/")
print a[len-]
}'` log_info "Deleting $NODE_SERVICES_DIR/$serviceFolder/node_modules"
rm -rf $NODE_SERVICES_DIR/$serviceFolder/node_modules
done for j in `find $NODE_MODULES_DIR -maxdepth -name package.json`
do
moduleFolder=`echo $j | awk '{
len=split($, a, "/")
print a[len-]
}'` log_info "Deleting $NODE_MODULES_DIR/$moduleFolder/node_modules"
rm -rf $NODE_MODULES_DIR/$moduleFolder/node_modules
done log_info "Deleting $WEB_UI_DIR/node_modules"
rm -rf $WEB_UI_DIR/node_modules log_info "Done cleanup."
separator
log_info "Starting all services..."
init_seeding
start
seed_oudls_orels_restapi exit
elif [ "$1" == "--seed" ]; then
cleanup
log_info "Done cleanup." separator
log_info "Starting all services..."
init_seeding
start
seed_oudls_orels_restapi exit
elif [ "$1" == "--seedonly" ]; then
NPM_INSTALL=false
init_seeding
#$SCRIPTS_DIR/restart usercontextstore recipestore recipecatalogstore connectorstore ocdlstore
seed_oudls_orels_restapi exit
elif [ "$1" == "--update" ]; then
log_info "Starting all services."
start
exit
elif [ -z $ ]; then
NPM_INSTALL=false
log_info "Starting all services"
log_warn "***npm update is disabled. You may not get the latest published dependencies."
sleep
start
exit
else
for j in $*
do
if [ "$j" == "webui" ]; then
setup_service_env ui $PRODUCT_DIR/web
start_node_service ui $PRODUCT_DIR/web
continue
fi serviceFolder="no-service-found"
get_service_folder $NODE_SERVICES_DIR $j if [ -d $NODE_SERVICES_DIR/$serviceFolder ]; then
setup_service_env $serviceFolder
start_node_service $serviceFolder
else
get_service_folder $NODE_CONNECTORS_DIR $j
if [ -d $NODE_CONNECTORS_DIR/$serviceFolder ]; then
setup_service_env $serviceFolder $NODE_CONNECTORS_DIR
start_node_service $serviceFolder $NODE_CONNECTORS_DIR
else
get_service_folder $NODE_AUX_DIR $j
if [ -d $NODE_AUX_DIR/$serviceFolder ]; then
#setup_service_env $serviceFolder $NODE_AUX_DIR
start_node_service $serviceFolder $NODE_AUX_DIR --no-validation
else
log_error "'$j' is not a valid service id. Valid service id list: "
for i in `find $NODE_SERVICES_DIR -maxdepth -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo " $pkgServiceName"
done
for i in `find $NODE_CONNECTORS_DIR -maxdepth -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo " $pkgServiceName"
done
for i in `find $NODE_AUX_DIR -maxdepth -name package.json `
do
pkgServiceName=`python $SCRIPTS_DIR/lib/getservicename.py $i`
echo " $pkgServiceName"
done
fi
fi
fi
done
exit
fi # eof
Java Web Services (1) - 第1章 Web服务快速入门的更多相关文章
- Web模糊测试:WFuzz的坑和快速入门
转载自 FreeBuf.COM 首先说下我对wfuzz这个工具的简单介绍和理解.工具主要是做web模糊测试,最开始做fuzz我是自己写个脚本配合一些常用工具来测,后来看见这款工具因为是比较简单吧,学习 ...
- 【转载】Gradle学习 第九章:Groovy快速入门
转载地址:http://ask.android-studio.org/?/article/17 To build a Groovy project, you use the Groovy plugin ...
- Java Web Services (2) - 第2章 启动日志分析
ZHAOFLIU-Mac:dev liuzhaofu$ ./start --seed########################################################## ...
- 《RESTful Web Services》第四章 设计URI
引言 URI是跨越Web的资源描述符,一个URI由以下内容组成——协议.主机.端口号.路径
- 《RESTful Web Services》第三章 设计表述
3.1 如何使用实体头来注解表述 表述不仅仅是以某种格式序列化后的数据,它是一连串字节加上用于描述那些字节的元数据. Content-Type,用于描述表述类型.这个标头告诉接收方如何 ...
- Spring企业级程序设计 • 【第5章 Spring MVC快速入门】
全部章节 >>>> 本章目录 5.1 Spring MVC设计概述及其框架结构 5.1.1 Spring MVC介绍 5.1.1 Spring MVC优势 5.1.2 S ...
- 一篇文章带你快速入门createjs
开始用createjs这个框架的时候,发现网上的相关教程还是挺少的,所以写一篇文章,方便日后查看. createjs简介 官网:http://www.createjs.cc/ createjs中包 ...
- [AI]SKLearn章1 快速入门
SciKit learn的简称是SKlearn,是一个python库,专门用于机器学习的模块. SKlearn包含的机器学习方式: 分类,回归,无监督,数据降维,数据预处理等等,包含了常见的大部分机器 ...
- 一篇文章让你快速入门 学懂Shell脚本
Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...
随机推荐
- 演练2-1:创建MVC默认项目
在VS2012中点击“文件 | 新项目”,在弹出对话框中选择“Visual C# | Web | ASP.NET MVC 4 Web应用程序”. 在弹出的模板对话框中选择“Internet应用程序”和 ...
- [置顶] Oracle 11g ASM:如何在 ASMCMD 命令行工具中创建 Oracle ACFS 文件系统
实验环境:Oracle 11g R2 RAC (11.2.0.3.5) Oracle Enterprise Linux 5.6 x86 1.创建 ASM 磁盘组 在两节点 ...
- PHP - 接口&抽象类
什么时候使用抽象类什么时候使用接口? .如果要创建一个模型,这个模型将由一些紧密相关的对象采用,就可以使用抽象类.如果要创建将由一些不相关对象采用的功能,就使用接口. .如果必须从多个来源继承行为,就 ...
- jquery Deferred使用经验
这周做了个小活动(http://aoqi.100bt.com/zt-2016duanzi/index.html),刚开始时候没看好需求,逻辑都写一块了 最后各种坑要填补,从中也获取了些经验和教训,下面 ...
- Windows Phone 9再见了!
因为需要准备注册电气工程师考试,因此Windows Phone 8初学者教程的翻译只能就此打住了,在考完后,也许就是Windows Phone 9发布的时候还会回来! Bye bye!
- hdu - 3572 - Task
题意:有N个作业,M台机器,每个作业1天只能同1台机器运行,每台机器1天只能运行1个作业,第i个作业需要pi天完成,且只能从Si到Ei中选Pi天,问能否完成所有作业(T <= 20, N< ...
- hihoCoder 1014trie树(字典树)
hihoCoder 1014 题目提示已经很清楚了~ 贴代码…… #include <iostream> #include <cstdio> #include <cstr ...
- java设计模式之观察者模式(9)
Java观察者模式的浅析 简单地说,观察者模式定义了一个一对多的依赖关系,让一个或多个观察者对象监察一个主题对象.这样一个主题对象在状态上的变化能够通知所有的依赖于此对象的那些观察者对象,使这些观察者 ...
- 【FAQ】SpingMVC实现集合參数(Could not instantiate bean class [java.util.List])
需求,要求批量新增或者改动一个List,在Spring MVC中是不支持以下代码的写法 @RequestMapping(value = "/update", method = Re ...
- Python标准库:内置函数dict(**kwarg)
本函数是从一个字典參数构造一个新字典.參数kwarg是键值对的字典參数.以两个*开头的參数.就会收集成字典形式. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, t ...