DBA Data[Home] [Help]

PACKAGE BODY: APPS.FND_GSM_UTIL

Source


1 package body fnd_gsm_util as
2 /* $Header: AFCPGUTB.pls 120.8 2008/01/11 16:20:28 tkamiya ship $ */
3 
4 
5   ctx_file_clob clob := null;
6   contextfile_handle varchar2(128);
7 
8   --
9   -- GENERIC_ERROR (Internal)
10   --
11   -- Set error message and raise exception for unexpected sql errors.
12   --
13   procedure GENERIC_ERROR(routine in varchar2,
14                           errcode in number,
15                           errmsg in varchar2) is
16     begin
17       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
18       fnd_message.set_token('ROUTINE', routine);
19       fnd_message.set_token('ERRNO', errcode);
20       fnd_message.set_token('REASON', errmsg);
21     end;
22 
23   /* returns TRUE if FND_DATABASES table exists else FALSE */
24   function db_model_exists return boolean is
25     TableNotFound EXCEPTION;
26     PRAGMA EXCEPTION_INIT(TableNotFound, -942);
27     cnt number;
28   begin
29     execute immediate 'select count(*) from fnd_databases' into cnt;
30 
31     return TRUE;
32 
33    exception
34       when TableNotFound then
35          return FALSE;
36 
37   end;
38 
39 
40   --
41   -- procedure
42   --   Append_ctx_fragment
43   --
44   -- Purpose
45   --   Used to upload a context file into a clob for parsing.
46   --   A temporary clob is created on the first call.  This clob
47   --   will be freed when the upload_context_file procedure is called.
48   --
49   -- In Arguments:
50   --   buffer - Context file, or fragment of a context file.
51   --
52   -- Out Arguments:
53   --   retcode - 0 on success, >0 on error.
54   --   message - Error message, up to 2000 bytes.
55   --
56   procedure append_ctx_fragment(buffer  in  varchar2,
57                                 retcode out nocopy number,
58                                 message out nocopy varchar2) is
59     buffer_length number;
60   begin
61 
62     -- Create the temp clob if necessary
63     if ctx_file_clob is null then
64       dbms_lob.createtemporary(ctx_file_clob, true, dbms_lob.session);
65     end if;
66 
67     -- Append the buffer to the clob
68     buffer_length := lengthb(buffer);
69     dbms_lob.writeappend (ctx_file_clob, buffer_length, buffer);
70     retcode := 0;
71 
72   exception
73     when others then
74       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
75       fnd_message.set_token('ROUTINE', 'FND_GSM_UTIL.APPEND_CTX_FRAGMENT');
76       fnd_message.set_token('ERRNO', SQLCODE);
77       fnd_message.set_token('REASON', SQLERRM);
78       message :=  fnd_message.get;
79       retcode := 1;
80 
81   end append_ctx_fragment;
82 
83 
84 
85   --
86   -- Procedure
87   --   upload_context_file
88   --
89   -- Purpose
90   --   Parse the context file stored in the temporary clob and create
91   --   the appropriate service instance definitions for GSM.  The clob is
92   --   created by the append_ctx_fragment
93   --
94   -- In Arguments:
95   --   filepath - Full path to the context file.  Used for bookkeeping.
96   --   context_type - 'APPS' Application middle tier,
97   --                  'DATABASE' Database context
98   --   file_type  - 'CONTEXT' - Instantiated Context file
99   --                'TEMPLATE' - Template file for Context file.
100   --
101   -- Out Arguments:
102   --   retcode - 0 on success, >0 on error.
103   --   message - Error message, up to 2000 bytes.
104   --
105   procedure upload_context_file(filepath in varchar2,
106                                 retcode out nocopy number,
107                                 message out nocopy varchar2,
108 				context_type in varchar2 default 'APPS',
109 				file_type in varchar2 default 'CONTEXT') is
110   primary_node  varchar2(30);
111   platform_name varchar2(30);
112   domain_name   varchar2(64);
113   tmp_node_name varchar2(100);
114   reports_port  varchar2(20);
115   forms_port    varchar2(20);
116   met_data_port varchar2(20);
117   met_req_port  varchar2(20);
118   web_port      varchar2(20);
119   web_pls_port  varchar2(20);
120   apache_top    varchar2(512);
121   web_pid_file  varchar2(510);
122   web_pls_pid_file  varchar2(510);
123   mod_pls_exists boolean;
124   tech_stack    varchar2(128);
125   logs_dir      varchar2(510);
126 
127   message_name varchar2(30);
128   service_name varchar2(255);
129   short_name   varchar2(30);
130   ctrl_script  varchar2(512);
131   port         varchar2(20);
132   status       varchar2(30);
133   service_handle varchar2(8);
134   parameters     varchar(2000);
135   process_name   varchar2(512);
136   process_log    varchar2(512);
137   reports_short_name varchar2(128);
138 
139   queue_id          number;
140   queue_appl_id     number;
141   fcq_enabled_flag  varchar2(1);
142 
143   my_parser    xmlParser.parser;
144   my_doc       xmlDOM.DOMDocument;
145   my_nodelist  xmlDOM.DOMNodeList;
146   my_nodelist2 xmlDOM.DOMNodeList;
147   my_contextlist xmlDOM.DOMNodeList;
148   my_node      xmlDOM.DOMNode;
149   my_node2     xmlDOM.DOMNode;
150   my_context   xmlDOM.DOMNode;
151   my_nodemap   xmlDOM.DOMNamedNodeMap;
152   my_attr      xmlDOM.DOMAttr;
153   my_element   xmlDOM.DOMElement;
154   server_type  varchar2(128);
155   oh_type      varchar2(128);
156   i            number;
157   listlength   number;
158   file_found   boolean := FALSE;
159   my_cdata     xmlDOM.DOMCharacterData;
160 
161   sr_num      varchar2(128);
162   f_sr_num    number;
163   db_sr_num   number;
164   file_ver     varchar2(128);
165   db_ver_str   varchar2(30);
166   db_ver       number;
167   f_ver        number;
168   context_name varchar2(512);
169   element_name varchar2(40);
170   missing_elem      exception;
171   exceeded_length   exception;
172   null_context_name exception;
173   metadata_file     exception;
174   filesys_low_ver   exception;
175   packagenotexists  exception;
176   PRAGMA EXCEPTION_INIT(packagenotexists, -06550);
177   sql_str      varchar2(1000);
178   db_name      varchar2(8);
179   db_domain    varchar2(255);
180   db_host      varchar2(255);
181   db_port      number;
182   db_sid       varchar2(255);
183   ret_val      number;
184   inst_name    varchar2(16);
185   inst_num     number;
186   c_null       varchar2(1);
187   n_null       number;
188   v_result     integer;
189   comp         number;
190 
191   begin
192 
193     if ctx_file_clob is null then
194       retcode := 1;
195       message := 'Developer error: Must call append_ctx_fragment first.';
196       return;
197     end if;
198 
199     /* Parse Context File */
200     my_parser := xmlparser.newParser;
201     xmlparser.parseClob(my_parser, ctx_file_clob);
202 --    dbms_lob.freetemporary(ctx_file_clob);
203 --    ctx_file_clob := null;
204     my_doc := xmlparser.getdocument(my_parser);
205 
206 
207     /* TO DO:  Below we search for each element top down.   The
208      * performance of this procedure can be increased if we use
209      * our knowledge of the file to avoid restarting at the top
210      * for each element.
211      * I don't know how significant of an improvement it will be.
212      * Since this program will run very infrequently, we will defer
213      * this effort.
214      */
215 
216     /* Get version */
217     my_contextlist := xmldom.getElementsByTagName(my_doc, 'oa_context');
218     listlength := xmldom.getLength(my_contextlist);
219     if listlength < 1 then
220       -- may be it is metadata file
221       my_contextlist := xmldom.getElementsByTagName(my_doc,'oa_context_doc');
222       listlength := xmldom.getLength(my_contextlist);
223       if listlength < 1 then
224          element_name := 'oa_context/oa_context_doc';
225          dbms_lob.freetemporary(ctx_file_clob);
226          ctx_file_clob := null;
227          raise missing_elem;
228       end if;
229     end if;
230 
231     my_node := xmldom.item(my_contextlist, 0);
232     my_element := xmldom.makeElement(my_node);
233     file_ver := xmldom.getAttribute(my_element, 'version');
234 
235     file_ver := substr(file_ver, instr(file_ver,':',1,1) + 1);
236 
237     file_ver := rtrim(file_ver, '$');
238     file_ver := ltrim(rtrim(file_ver));
239   --  f_ver := to_number(NVL(substr(file_ver,instr(file_ver,'.',1,1) +1),0));
240 
241     /* Get Context name */
242     if ( file_type = 'TEMPLATE' ) then
243       context_name := 'TEMPLATE';
244     else
245       my_contextlist := xmldom.getElementsByTagName(my_doc, 'oa_context_name');
246       listlength := xmldom.getLength(my_contextlist);
247       if listlength < 1 then
248         context_name := 'METADATA';
249       else
250         my_node := xmldom.item(my_contextlist, 0);
251         my_node := xmldom.getFirstChild(my_node);
252         if xmldom.isNull(my_node) then
253            dbms_lob.freetemporary(ctx_file_clob);
254            ctx_file_clob := null;
255            raise null_context_name;
256         else
257            context_name := ltrim(rtrim(xmldom.getNodeValue(my_node)));
258         end if;
259       end if;
260     end if;
261 
262     if ( context_name in  ('METADATA', 'TEMPLATE') ) then
263        -- For metadata get node name from session
264        -- I don't think we need to handle no_data_found
265        select substr(machine, 1,
266 		     decode(instr(machine, '.', 1, 1),
267 				0, length(machine),
268 				instr(machine, '.', 1, 1)-1))
269 	 into primary_node
270          from v$session
271         where audsid=USERENV('SESSIONID');
272     else
273       /* Get Name of Node */
274       my_nodelist := xmldom.getElementsByTagName(my_doc, 'host');
275       listlength := xmldom.getLength(my_nodelist);
276       if listlength < 1 then
277         element_name := 'host';
278         dbms_lob.freetemporary(ctx_file_clob);
279         ctx_file_clob := null;
280         raise missing_elem;
281       end if;
282       my_node := xmldom.item(my_nodelist, 0);
283       my_node := xmldom.getFirstChild(my_node);
284       if xmldom.isNull(my_node) then
285          primary_node := null;
286       else
287          primary_node := ltrim(rtrim(xmldom.getNodeValue(my_node)));
288       end if;
289     end if;
290 
291     /* Get Serial Number from context file */
292     if ( context_name in ('METADATA', 'TEMPLATE')) then
293       sr_num := '0';
294     else
295       my_nodelist := xmldom.getElementsByTagName(my_doc, 'oa_context_serial');
296       listlength := xmldom.getLength(my_nodelist);
297       if listlength < 1 then
298         goto service_upld;
299       end if;
300       my_node := xmldom.item(my_nodelist, 0);
301       my_node := xmldom.getFirstChild(my_node);
302       if xmldom.isNull(my_node) then
303         sr_num := null;
304       else
305         sr_num := ltrim(rtrim(xmldom.getNodeValue(my_node)));
306       end if;
307     end if;
308 
309     f_sr_num := to_number(NVL(sr_num,0));
310 
311     -- try inserting row into fnd_oam_context_files table
312     -- fnd_oam_context_files.status values: (H)istory,(S)uccess,(F)ailure
313     --pbasha added for synchronization
314          if  (contextfile_handle is null)
315           then
316          dbms_lock.allocate_unique('contextfile_lock',contextfile_handle);
317           end if;
318     v_result := dbms_lock.request(lockhandle=>contextfile_handle,release_on_commit=>TRUE);
319     begin
320        select NVL(serial_number, 0), version into db_sr_num, db_ver_str
321          from fnd_oam_context_files
322         where (node_name = primary_node
323           and  path = filepath
324           and  name not in ('METADATA','TEMPLATE')
325           and  (status is null or upper(status) in ('S','F')))
326            or (path = filepath
327           and  name in ( 'METADATA', 'TEMPLATE'))
328           for update;
329 
330           file_found := TRUE;
331           v_result := dbms_lock.release(contextfile_handle);
332     exception
333        when no_data_found then
334           file_found := FALSE;
335     end;
336 
337 --    db_ver := to_number(NVL(substr(db_ver_str,instr(db_ver_str,'.',1,1) +1),0));
338 
339     if ( not file_found ) then
340         insert into fnd_oam_context_files
341                      (name, version, path, last_synchronized,
342                       text, creation_date, created_by, last_update_date,
343                       last_updated_by, last_update_login, node_name,
344                       status, ctx_type)
345               values (context_name, file_ver, filepath, sysdate,
346                       ctx_file_clob, sysdate, FND_GLOBAL.user_id, sysdate,
347                       FND_GLOBAL.user_id, FND_GLOBAL.login_id, primary_node,
348                       'S',
349                       DECODE(context_type, 'APPS', 'A', 'DATABASE','D','A'));
350        v_result := dbms_lock.release(contextfile_handle);
351 
352     else
353       -- overwrite the database copy of context file if the serial # of the
354       -- file being uploaded is >= to the copy in the DB.
355       -- or context version is > version in file(due to template patch)
356       -- for METADATA file we don't need to check for serial number.
357 
358          comp  := version_check(file_ver,db_ver_str);
359       if ( (( (f_sr_num >= db_sr_num) OR (comp > 0))
360               AND (context_name not in ('METADATA', 'TEMPLATE'))
361            OR context_name in ('METADATA', 'TEMPLATE'))) then
362 	 -- bug6739946
363 	 -- added status to be set to S to mark upload of template
364 	 -- to already existing row
365          update fnd_oam_context_files
366             set path = filepath,
367                 last_synchronized = sysdate,
368                 text = ctx_file_clob,
369                 last_update_date = sysdate,
370                 last_updated_by  = fnd_global.user_id,
371                 last_update_login = fnd_global.login_id,
372                 node_name = primary_node,
373                 version = file_ver,
374 		status = 'S'
375           where (node_name = primary_node
376             and  path = filepath
377             and  name not in ('METADATA', 'TEMPLATE')
378             and  (status is null or upper(status) in ('S','F')))
379              or (path = filepath
380             and  name in ( 'METADATA', 'TEMPLATE'));
381        else
382           raise filesys_low_ver;
383        end if;
384     end if;
385 
386     <<service_upld>>
387 
388     -- clear clob
389     dbms_lob.freetemporary(ctx_file_clob);
390     ctx_file_clob := null;
391     if ( context_name in ('METADATA', 'TEMPLATE')
392            or context_type = 'DATABASE') then
393        -- for metadata we don't need to register service definitions.
394        -- for database context file we don't need to register service def
395        raise metadata_file;
396     end if;
397 
398     -- if FND_DATABASES data model exists then try to insert topology info
399     if ( db_model_exists ) then
400     begin
401       sql_str := 'declare b boolean; Begin b := ' ||
402 		'fnd_conc_database.register_database(:1,:2,:3,:4,:5); ' ||
403 		' if (b) then :6 := 1; else :6 := 0; end if; end;';
404       select substr(value,1,8) into db_name
405         from v$parameter where name='db_name';
406       select substr(value,1,255) into db_domain
407         from v$parameter where name='db_domain';
408 
409       execute immediate sql_str using in db_name,
410                   in db_domain, in c_null, in c_null, in n_null, out ret_val;
411 
412 
413       /* Get db_host value */
414       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbhost');
415       listlength := xmldom.getLength(my_nodelist);
416       if listlength < 1 then
417         element_name := 'dbhost';
418         raise missing_elem;
419       end if;
423         db_host := null;
420       my_node := xmldom.item(my_nodelist, 0);
421       my_node := xmldom.getFirstChild(my_node);
422       if xmldom.isNull(my_node) then
424       else
425         db_host := ltrim(rtrim(xmldom.getNodeValue(my_node)));
426       end if;
427 
428       /* Get db_sid value */
429       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbsid');
430       listlength := xmldom.getLength(my_nodelist);
431       if listlength < 1 then
432         element_name := 'dbsid';
433         raise missing_elem;
434       end if;
435       my_node := xmldom.item(my_nodelist, 0);
436       my_node := xmldom.getFirstChild(my_node);
437       if xmldom.isNull(my_node) then
438         db_sid := null;
439       else
440         db_sid := ltrim(rtrim(xmldom.getNodeValue(my_node)));
441       end if;
442 
443       /* Get db_port value */
444       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbport');
445       listlength := xmldom.getLength(my_nodelist);
446       if listlength < 1 then
447         element_name := 'dbport';
448         raise missing_elem;
449       end if;
450       my_node := xmldom.item(my_nodelist, 0);
451       my_node := xmldom.getFirstChild(my_node);
452       if xmldom.isNull(my_node) then
453         db_port := null;
454       else
455         db_port := to_number(ltrim(rtrim(xmldom.getNodeValue(my_node))));
456       end if;
457 
458       select instance_number,instance_name
459         into inst_num, inst_name
460         from v$instance;
461 
462       sql_str := 'declare b boolean; Begin b := ' ||
463 		'fnd_conc_database.register_instance( ' ||
464                 ':1,:2,:3,:4,:5,:6,:7,:8,:9); ' ||
465 		' if (b) then :10 := 1; else :10 := 0; end if; end;';
466       execute immediate sql_str using in db_name,
467 		in inst_name, in inst_num, in db_sid, in db_host, in db_port,
468 		in db_sid, in c_null, in c_null, out ret_val;
469 
470 
471       exception
472         /* Ignore package(FND_CONC_DATABASE) not available cases. */
473         when packagenotexists then
474            null;
475       end;
476      end if;
477 
478 
479 
480     /* Get Name of platform */
481     my_nodelist := xmldom.getElementsByTagName(my_doc, 'platform');
482     listlength := xmldom.getLength(my_nodelist);
483     if listlength < 1 then
484       element_name := 'platform';
485       raise missing_elem;
486     end if;
487     my_node := xmldom.item(my_nodelist, 0);
488     my_node := xmldom.getFirstChild(my_node);
489     if xmldom.isNull(my_node) then
490       platform_name := null;
491     else
492       platform_name := ltrim(rtrim(xmldom.getNodeValue(my_node)));
493     end if;
494 
495 
496     -- Tru64 UNIX Alpha need hostname.domain_name as node name
497 
498     if ( platform_name = 'UNIX Alpha' ) then
499 	    /* Get Name of domain */
500         my_nodelist := xmldom.getElementsByTagName(my_doc, 'domain');
501         listlength := xmldom.getLength(my_nodelist);
502         if listlength < 1 then
503           element_name := 'domain';
504           raise missing_elem;
505         end if;
506         my_node := xmldom.item(my_nodelist, 0);
507         my_node := xmldom.getFirstChild(my_node);
508         if xmldom.isNull(my_node) then
509           domain_name := null;
510         else
511           domain_name := substr(ltrim(rtrim(xmldom.getNodeValue(my_node))), 1, 64);
512 	    end if;
513 
514         tmp_node_name := primary_node || '.' || domain_name;
515         if (length(tmp_node_name) > 30) then
516             raise exceeded_length;
517         else
518             primary_node := tmp_node_name;
519         end if;
520     end if;
521 
522     /* Since port info is stored separately from the process info,
523        we first retrieve the ports, and then loop through the processes
524        to register the services. */
525 
526 
527     /* Get the Reports port */
528 
529     my_nodelist := xmldom.getElementsByTagName(my_doc, 'reports_port');
530     listlength := xmldom.getLength(my_nodelist);
531     if listlength < 1 then
532       element_name := 'reports_port';
533       -- To fix 4684481
534       -- raise missing_elem;
535     else
536       my_node := xmldom.item(my_nodelist, 0);
537       my_node := xmldom.getFirstChild(my_node);
538       if xmldom.isNull(my_node) then
539         reports_port := null;
540       else
541         reports_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
542       end if;
543     end if;
544 
545 
546 
547     /* Get the Forms port */
548 
549     my_nodelist := xmldom.getElementsByTagName(my_doc, 'forms_port');
550     listlength := xmldom.getLength(my_nodelist);
551     if listlength < 1 then
552       element_name := 'forms_port';
553       -- To fix bug 5151130
554       -- raise missing_elem;
555     end if;
556     my_node := xmldom.item(my_nodelist, 0);
557     my_node := xmldom.getFirstChild(my_node);
558     if xmldom.isNull(my_node) then
559       forms_port := null;
560     else
561       forms_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
562     end if;
563 
564 
565 
569     listlength := xmldom.getLength(my_nodelist);
566     /* Get the metrics request port */
567 
568     my_nodelist := xmldom.getElementsByTagName(my_doc, 'met_req_port');
570     if listlength < 1 then
571       element_name := 'met_req_port';
572       -- To fix bug 5151130
573       -- raise missing_elem;
574     end if;
575     my_node := xmldom.item(my_nodelist, 0);
576     my_node := xmldom.getFirstChild(my_node);
577     if xmldom.isNull(my_node) then
578       met_req_port := null;
579     else
580       met_req_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
581     end if;
582 
583 
584 
585     /* Get the metrics data port */
586 
587     my_nodelist := xmldom.getElementsByTagName(my_doc, 'met_data_port');
588     listlength := xmldom.getLength(my_nodelist);
589     if listlength < 1 then
590       element_name := 'met_data_port';
591       -- To fix bug 5151130
592       -- raise missing_elem;
593     end if;
594     my_node := xmldom.item(my_nodelist, 0);
595     my_node := xmldom.getFirstChild(my_node);
596     if xmldom.isNull(my_node) then
597       met_data_port := null;
598     else
599       met_data_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
600     end if;
601 
602 
603     /* Get the web port */
604 
605     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_port');
606     listlength := xmldom.getLength(my_nodelist);
607     if listlength < 1 then
608       element_name := 'web_port';
609       raise missing_elem;
610     end if;
611     my_node := xmldom.item(my_nodelist, 0);
612     my_node := xmldom.getFirstChild(my_node);
613     if xmldom.isNull(my_node) then
614       web_port := null;
615     else
616       web_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
617     end if;
618 
619     /* Get web pls port if any */
620 
621     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_port_pls');
622     listlength := xmldom.getLength(my_nodelist);
623     if listlength > 0 then
624        my_node := xmldom.item(my_nodelist, 0);
625        my_node := xmldom.getFirstChild(my_node);
626        if xmldom.isNull(my_node) then
627          web_pls_port := null;
628        else
629          web_pls_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
630        end if;
631     else
632        web_pls_port := null;
633     end if;
634 
635 
636     /* Get the Apache_Top */
637 
638     my_nodelist := xmldom.getElementsByTagName(my_doc, 'ORACLE_HOME');
639     listlength := xmldom.getLength(my_nodelist);
640     if listlength < 1 then
641       element_name := 'ORACLE_HOME oa_var=s_web_oh';
642       raise missing_elem;
643     end if;
644 
645     for i in 0..listlength-1 loop
646       my_node := xmldom.item(my_nodelist, i);
647       my_element := xmldom.makeElement(my_node);
648       oh_type := xmldom.getAttribute(my_element, 'oa_var');
649       exit when oh_type = 's_weboh_oh';
650     end loop;
651 
652     if (oh_type = 's_weboh_oh') then
653         my_node := xmldom.getFirstChild(my_node);
654         if xmldom.isNull(my_node) then
655           apache_top := null;
656         else
657           apache_top := ltrim(rtrim(xmldom.getNodeValue(my_node)));
658         end if;
659     else
660       element_name := 'ORACLE_HOME oa_var=s_web_oh';
661       raise missing_elem;
662     end if;
663 
664 
665     /* Get the web pid file if any, do not show this an error */
666 
667     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_pid_file');
668     listlength := xmldom.getLength(my_nodelist);
669     if listlength > 0 then
670        my_node := xmldom.item(my_nodelist, 0);
671        my_node := xmldom.getFirstChild(my_node);
672        if xmldom.isNull(my_node) then
673          web_pid_file := null;
674        else
675          web_pid_file := ltrim(rtrim(xmldom.getNodeValue(my_node)));
676        end if;
677     end if;
678 
679     /* Get logs_dir value, used for reports */
680     my_nodelist := xmldom.getElementsByTagName(my_doc, 'logs_dir');
681     listlength := xmldom.getLength(my_nodelist);
682     if listlength > 0 then
683        my_node := xmldom.item(my_nodelist, 0);
684        my_node := xmldom.getFirstChild(my_node);
685        if xmldom.isNull(my_node) then
686          logs_dir := null;
687        else
688          logs_dir := ltrim(rtrim(xmldom.getNodeValue(my_node)));
689        end if;
690     else
691        element_name := 'logs_dir oa_var=s_logdir';
692        raise missing_elem;
693     end if;
694 
695 
696     /* Get techstack value */
697     my_nodelist := xmldom.getElementsByTagName(my_doc, 'config_option');
698     listlength := xmldom.getLength(my_nodelist);
699     if listlength < 1 then
700        tech_stack := null;
701     end if;
702 
703     for i in 0..listlength-1 loop
704       my_node := xmldom.item(my_nodelist, i);
705       my_element := xmldom.makeElement(my_node);
706       oh_type := xmldom.getAttribute(my_element, 'oa_var');
707       exit when oh_type = 's_techstack';
708     end loop;
709 
710     if (oh_type = 's_techstack') then
714         else
711         my_node := xmldom.getFirstChild(my_node);
712         if xmldom.isNull(my_node) then
713           tech_stack := null;
715           tech_stack := ltrim(rtrim(xmldom.getNodeValue(my_node)));
716         end if;
717     else
718         tech_stack := null;
719     end if;
720 
721 
722     /* find out mod_pls process is exists or not. If exists then pass
723        'core' to Apache control script, else old stuff.
724      */
725 
726     mod_pls_exists := FALSE;
727     my_nodelist := xmldom.getElementsByTagname(my_doc, 'oa_process');
728     listlength := xmldom.getLength(my_nodelist);
729     for i in 0..(listlength -1 ) loop
730       my_node := xmldom.item(my_nodelist, i);
731       my_element := xmldom.makeElement(my_node);
732       server_type := xmldom.getAttribute(my_element, 'type');
733 
734       if (server_type = 'apache_pls' ) then
735          mod_pls_exists := TRUE;
736       end if;
737     end loop;
738 
739 
740     /* Register Services */
741 
742     my_nodelist := xmldom.getElementsByTagName(my_doc, 'oa_process');
743     listlength := xmldom.getLength(my_nodelist);
744     for i in 0..(listlength - 1) loop
745       my_node := xmldom.item(my_nodelist, i);
746       my_element := xmldom.makeElement(my_node);
747       server_type := xmldom.getAttribute(my_element, 'type');
748 
749 
750       /* Get the process name */
751       my_nodelist2 := xmldom.getElementsByTagName(my_element,
752                                                       'oa_process_name');
753       listlength := xmldom.getLength(my_nodelist2);
754       if listlength < 1 then
755         element_name := 'oa_process_name';
756         raise missing_elem;
757       end if;
758       my_node2 := xmldom.item(my_nodelist2, 0);
759       my_node2 := xmldom.getFirstChild(my_node2);
760       if xmldom.isNull(my_node2) then
761         process_name := null;
762       else
763         process_name := ltrim(rtrim(
764                             xmldom.getNodeValue(my_node2), '" '), '" ');
765       end if;
766 
767 
768       /* Get the control script name. */
769       my_nodelist2 := xmldom.getElementsByTagName(my_element,
770                                                  'ctrl_script');
771       listlength := xmldom.getLength(my_nodelist2);
772       if listlength < 1 then
773         element_name := 'ctrl_script';
774         raise missing_elem;
775       end if;
776       my_node2 := xmldom.item(my_nodelist2, 0);
777       my_node2 := xmldom.getFirstChild(my_node2);
778       if xmldom.isNull(my_node2) then
779         ctrl_script := null;
780       else
781         ctrl_script := ltrim(rtrim(
782                              xmldom.getNodeValue(my_node2), '" '), '" ');
783       end if;
784 
785 
786       /* Get the log file name name. */
787       my_nodelist2 := xmldom.getElementsByTagName(my_element,
788                                                  'oa_process_log');
789       listlength := xmldom.getLength(my_nodelist2);
790       if listlength < 1 then
791         element_name := 'oa_process_log';
792         raise missing_elem;
793       end if;
794       my_node2 := xmldom.item(my_nodelist2, 0);
795       my_node2 := xmldom.getFirstChild(my_node2);
796       if xmldom.isNull(my_node2) then
797         process_log := null;
798       else
799         process_log := ltrim(rtrim(
800                              xmldom.getNodeValue(my_node2), '" '), '" ');
801       end if;
802 
803 
804       if server_type in ('apache','forms','met_cl','met_srv',
805                          'reports', 'apache_pls') then
806 
807         /* Determine full name, port, and short name */
808         if server_type  = 'apache' then
809           message_name := 'GSM-APACHE SVC INST';
810           port := web_port;
811           short_name := 'APACHE_'||substrb(primary_node, 1, 12)||'_'||
812                         substr(port, 1, 8);
813           service_handle := 'Apache';
814 
815           /* if mod_pls_exists then pass 'core' to regular apache */
816           if (mod_pls_exists) then
817              parameters :=  'START=PATH,'|| ctrl_script||' start core;'||
818                          'STOP=PATH,'||ctrl_script||' stop core;'||
819                          'LOG='||process_log||';';
820           else
821              parameters := 'START=PATH,'|| ctrl_script||' start;'||
822                            'STOP=PATH,'||ctrl_script||' stop;'||
823                            'LOG='||process_log||';';
824           end if;
825 
826           /* determine pid file for ias10 */
827           if ( tech_stack = 'ias10' ) then
828               if ( substr(upper(platform_name),
829                            length(platform_name)-1) <> 'NT' ) then
830 		web_pid_file := apache_top || '/Apache/Apache/logs/httpds.pid';
831               else
832                 web_pid_file := apache_top || '/Apache/Apache/logs/httpd.pid';
833               end if;
834           end if;
835 
836           if (apache_top is not null) then
837             /* if pid file is there then use it */
838             if ( web_pid_file is not null) then
839               parameters := parameters || 'PID=FILE,PATH,' ||web_pid_file||
840                             ';'|| 'SERVICE='||process_name;
844                            'SERVICE='||process_name;
841             else
842               parameters := parameters || 'PID=FILE,PATH,'||apache_top||
843                            '/Apache/Apache/logs/httpds.pid;'||
845             end if;
846           else
847             parameters := parameters || 'SERVICE='||process_name;
848           end if;
849 
850         elsif server_type  = 'apache_pls' then
851           message_name := 'GSM-MOD PLS SVC INST';
852           port := web_pls_port;
853           short_name := 'APACHE_'||substrb(primary_node, 1, 12)||'_'||
854                         substr(port, 1, 8);
855           service_handle := 'Apache';
856 
857           parameters :=  'START=PATH,'|| ctrl_script||' start pls;'||
858                          'STOP=PATH,'||ctrl_script||' stop pls;'||
859                          'LOG='||process_log||';';
860 
861           if (apache_top is not null) then
862               parameters := parameters || 'PID=FILE,PATH,' ||apache_top||
863                             '/Apache/Apache/logs/httpd_pls.pid' ||
864                             ';'|| 'SERVICE='||process_name;
865           else
866             parameters := parameters || 'SERVICE='||process_name;
867           end if;
868 
869           /* The PID file won't be used on NT, so the Unix style
870              directory delimiters above should not be a problem. */
871 
872 
873         elsif server_type  = 'forms' then
874           message_name := 'GSM-FORMS SVC INST';
875           port := forms_port;
876           short_name := 'FORMS_'||substr(primary_node, 1, 12)||'_'||
877                          substrb(port, 1, 8);
878           service_handle := 'FormsL';
879           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
880                          'STOP=PATH,'||ctrl_script||' stop;'||
881                          'LOG='||process_log||';'||
882                          'PID=FIND,f60srvm,'||primary_node||'_'||port||';'||
883                          'SERVICE='||process_name;
884 
885 
886         elsif server_type  = 'met_cl' then
887           message_name := 'GSM-METRICS CL SVC INST';
888           port := met_data_port;
889           short_name := 'MET_CL_'||substr(primary_node, 1, 12)||'_'||
890                          substrb(met_data_port, 1, 8);
891           service_handle := 'FormsMC';
892           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
893                          'STOP=PATH,'||ctrl_script||' stop;'||
894                          'LOG='||process_log||';'||
895                          'PID=FIND,d2lc60,'||primary_node||
896                          ','||met_data_port||';'||
897                          'SERVICE='||process_name;
898 
899         elsif server_type  = 'met_srv' then
900           message_name := 'GSM-METRICS SRV SVC INST';
901           port := met_req_port;
902           short_name := 'MET_SRV_'||substr(primary_node, 1, 12)||'_'||
903                          substrb(met_req_port, 1, 8);
904           service_handle := 'FormsMS';
905           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
906                          'STOP=PATH,'||ctrl_script||' stop;'||
907                          'LOG='||process_log||';'||
908                          'PID=FIND,d2ls60,'||met_data_port||
909                          ','||met_req_port||';'||
910                          'SERVICE='||process_name;
911 
912         elsif server_type  = 'reports' then
913           /* Note the service name has the format:
914            *    "Oracle Reports Server [short_name]"
915            * Here we extract the short_name.                     */
916           reports_short_name := rtrim(substr(process_name,
917                                        instr(process_name,'[')+1), '] ');
918           /* replace Rep60 with REP60
919            * using parse mechanism instead of replace func
920            */
921           reports_short_name := upper(substr(reports_short_name, 1,
922                                        instr(reports_short_name,'_'))) ||
923 				substr(reports_short_name,
924 					instr(reports_short_name,'_')+1);
925 
926 	  /* 3578632 adrepctl now uses REP60_%s_dbSid% as the REPSERV_NAME,
927            *  instead of s_reptname as we do here.                          */
928 	  if (db_sid is not null ) then
929 		reports_short_name := 'REP60_' || db_sid;
930 	  end if;
931 
932           message_name := 'GSM-REPORTS SRV SVC INST';
933           port := reports_port;
934           short_name := 'REPORTS_'||substrb(primary_node, 1, 12)||'_'||
935                         substr(port, 1, 8);
936           service_handle := 'RepServ';
937 
938           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
939                          'STOP=PATH,'||ctrl_script||' stop;'||
940                          'LOG='||process_log||';'||
941                          'PID=FILE,PATH,'||logs_dir||'/'||reports_short_name||
942 			 '.PID'||';'||'SERVICE='||process_name;
943        end if;
944 
945         /* Is the service enabled? */
946         my_nodelist2 := xmldom.getElementsByTagName(my_element,
947                                                     'oa_process_status');
948         listlength := xmldom.getLength(my_nodelist2);
949         if listlength < 1 then
950           element_name := 'oa_process_status';
951           raise missing_elem;
952         end if;
953         my_node2 := xmldom.item(my_nodelist2, 0);
957         else
954         my_node2 := xmldom.getFirstChild(my_node2);
955         if xmldom.isNull(my_node2) then
956           status := 'disabled';
958           status := ltrim(rtrim(xmldom.getNodeValue(my_node2), '" '), '" ');
959         end if;
960 
961         if status = 'enabled' then
962           /* Does the service exist in GSM? */
963           begin
964             select concurrent_queue_id, application_id, enabled_flag
965               into queue_id, queue_appl_id, fcq_enabled_flag
966               from fnd_concurrent_queues
967              where application_id = 0
968                and concurrent_queue_name = short_name;
969 
970             /* Yes, the service exists.  Otherwise, there would *
971              * have been a no_data_found exception.             */
972 
973 
974             /* Update the workshift parameters. */
975 
976             update fnd_concurrent_queue_size
977                set service_parameters = parameters
978              where concurrent_queue_id = queue_id
979                and queue_application_id = queue_appl_id;
980 
981 
982           exception
983 
984             when no_data_found then
985               /* The service is not registered with GSM */
986               /* Do the registration now. */
987               fnd_message.set_name('FND', message_name);
988               fnd_message.set_token('NODE', primary_node);
989               fnd_message.set_token('PORT', port);
990               service_name := substrb(fnd_message.get, 1, 240);
991 
992               begin
993                 fnd_manager.register_si(manager => service_name,
994                                         application => 'FND',
995                                         short_name => short_name,
996                                         service_handle => service_handle,
997                                         primary_node => upper(primary_node),
998                                         language_code => userenv('LANG'));
999 
1000 
1001                 fnd_manager.assign_work_shift(
1002                                         manager_short_name => short_name,
1003                                         manager_application => 'FND',
1004                                         processes => 1,
1005                                         sleep_seconds => 1,
1006                                         work_shift_id => 0,
1007                                         svc_params => parameters);
1008 
1009                  /* Now update enabled flag to 'N' */
1010                 select concurrent_queue_id, application_id, enabled_flag
1011                   into queue_id, queue_appl_id, fcq_enabled_flag
1012                   from fnd_concurrent_queues
1013                  where application_id = 0
1014                    and concurrent_queue_name = short_name;
1015 
1016                 update fnd_concurrent_queues
1017                    set enabled_flag = 'N'
1018                  where concurrent_queue_id = queue_id
1019                    and application_id = queue_appl_id;
1020 
1021               exception
1022                 when program_error then
1023                   message := fnd_manager.message;
1024 
1025                   if (not xmldom.isNull(my_doc)) then
1026                      xmldom.freeDocument (my_doc);
1027                   end if;
1028                   xmlparser.freeParser (my_parser);
1029 
1030                   retcode := 1;
1031                   return;
1032 
1033                 when others then
1034                   fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
1035                   fnd_message.set_token('ROUTINE',
1036                                         'FND_GSM_UTIL.upload_context_file');
1037                   fnd_message.set_token('ERRNO', SQLCODE);
1038                   fnd_message.set_token('REASON', SQLERRM);
1039                   message :=  fnd_message.get;
1040 
1041                   if (not xmldom.isNull(my_doc)) then
1042                      xmldom.freeDocument (my_doc);
1043                   end if;
1044                   xmlparser.freeParser (my_parser);
1045 
1046                   retcode := 1;
1047                   return;
1048               end;
1049           end;
1050 
1051         else /* Service is disabled in context file. */
1052 
1053           /* Update fcq if necessary */
1054           update fnd_concurrent_queues
1055              set enabled_flag = 'N'
1056            where application_id = 0
1057              and concurrent_queue_name = short_name;
1058 
1059         end if;
1060 
1061       end if;
1062 
1063     end loop;
1064 
1065     if (not xmldom.isNull(my_doc)) then
1066        xmldom.freeDocument (my_doc);
1067     end if;
1068     xmlparser.freeParser (my_parser);
1069 
1070     retcode := 0;
1071 
1072   exception
1073     when metadata_file then
1074       if (not xmldom.isNull(my_doc)) then
1075          xmldom.freeDocument (my_doc);
1076       end if;
1077       xmlparser.freeParser (my_parser);
1078 
1079       retcode := 0;
1080 
1081     when null_context_name then
1082       fnd_message.set_name('FND', 'GSM-CTX NULL CONTEXT');
1083       fnd_message.set_token('FILE', filepath);
1084       message := fnd_message.get;
1085 
1086       if (not xmldom.isNull(my_doc)) then
1090 
1087          xmldom.freeDocument (my_doc);
1088       end if;
1089       xmlparser.freeParser (my_parser);
1091       retcode := 1;
1092 
1093     when filesys_low_ver then
1094       fnd_message.set_name('FND', 'GSM-CTX FILE SYS COPY LOW');
1095       fnd_message.set_token('FILE', filepath);
1096       message := fnd_message.get;
1097 
1098       if (not xmldom.isNull(my_doc)) then
1099          xmldom.freeDocument (my_doc);
1100       end if;
1101       xmlparser.freeParser (my_parser);
1102 
1103       retcode := 1;
1104 
1105     when missing_elem then
1106       fnd_message.set_name('FND', 'GSM-CTX ELEMENT MISSING');
1107       fnd_message.set_token('ELEMENT', element_name);
1108       fnd_message.set_token('FILE', filepath);
1109       message :=  fnd_message.get;
1110 
1111       if (not xmldom.isNull(my_doc)) then
1112          xmldom.freeDocument (my_doc);
1113       end if;
1114       xmlparser.freeParser (my_parser);
1115 
1116       retcode := 1;
1117 
1118     when exceeded_length then
1119       fnd_message.set_name('FND', 'GSM-CTX NODE NAME TOO LONG');
1120       fnd_message.set_token('NODE_NAME', tmp_node_name);
1121       message := fnd_message.get;
1122 
1123       if (not xmldom.isNull(my_doc)) then
1124          xmldom.freeDocument (my_doc);
1125       end if;
1126       xmlparser.freeParser (my_parser);
1127 
1128       retcode := 1;
1129 
1130     when others then
1131       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
1132       fnd_message.set_token('ROUTINE', 'FND_GSM_UTIL.upload_context_file');
1133       fnd_message.set_token('ERRNO', SQLCODE);
1134       fnd_message.set_token('REASON', SQLERRM);
1135       message :=  fnd_message.get;
1136 
1137       if (not xmldom.isNull(my_doc)) then
1138          xmldom.freeDocument (my_doc);
1139       end if;
1140       xmlparser.freeParser (my_parser);
1141 
1142       retcode := 1;
1143 
1144   end upload_context_file;
1145 
1146 function version_check(version1 varchar,version2 varchar) return number is
1147          l_version1      varchar2(500) default version1 || '.';
1148 	 l_version2      varchar2(500) default version2 || '.';
1149 	 TYPE data IS TABLE OF varchar2(100);
1150          l_data1  data := data();
1151 	 l_data2  data := data();
1152 	 n number;
1153 	 m number;
1154 	 x number;
1155 	 p number;
1156 
1157   begin
1158 
1159       loop
1160            exit when l_version1 is null and l_version2 is null;
1161 	   if l_version1 is not null then
1162               n := instr( l_version1, '.' );
1163               l_data1.extend;
1164               l_data1(l_data1.count) := ltrim( rtrim( substr( l_version1, 1, n-1 ) ) );
1165               l_version1 := substr( l_version1, n+1 );
1166 	   end if;
1167 	   if l_version2 is not null then
1168 	      m := instr( l_version2, '.' );
1169               l_data2.extend;
1170               l_data2(l_data2.count) := ltrim( rtrim( substr( l_version2, 1, m-1 ) ) );
1171               l_version2 := substr( l_version2, m+1 );
1172 	    end if;
1173 
1174      end loop;
1175 
1176          if (l_data1.count < l_data2.count) then
1177                 x := l_data1.count;
1178           else
1179 	        x := l_data2.count;
1180            end if;
1181      for i in 1 .. x
1182         loop
1183           p:=  to_number(NVL(l_data1(i),0))-to_number(NVL(l_data2(i),0));
1184 	  if (p <> 0) then
1185 	      return p;
1186 	  end if;
1187        end loop;
1188          return l_data1.count - l_data2.count;
1189   end version_check;
1190 end fnd_gsm_util;