00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import omniORB.CORBA as CORBA
00019 import CosNaming
00020 import string
00021 import sys
00022 import traceback
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 class CorbaNaming:
00062 """
00063 """
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 def __init__(self, orb, name_server=None):
00082 self._orb = orb
00083 self._nameServer = ""
00084 self._rootContext = CosNaming.NamingContext._nil
00085 self._blLength = 100
00086
00087 if name_server:
00088 self._nameServer = "corbaloc::" + name_server + "/NameService"
00089 try:
00090 obj = orb.string_to_object(self._nameServer)
00091 self._rootContext = obj._narrow(CosNaming.NamingContext)
00092 if CORBA.is_nil(self._rootContext):
00093 print "CorbaNaming: Failed to narrow the root naming context."
00094
00095 except CORBA.ORB.InvalidName:
00096 self.__print_exception()
00097 print "Service required is invalid [does not exist]."
00098
00099 return
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 def __del__(self):
00115 return
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 def init(self, name_server):
00132 self._nameServer = "corbaloc::" + name_server + "/NameService"
00133 obj = self._orb.string_to_object(self._nameServer)
00134 self._rootContext = obj._narrow(CosNaming.NamingContext)
00135 if CORBA.is_nil(self._rootContext):
00136 raise MemoryError
00137
00138 return
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 def isAlive(self):
00156 try:
00157 if self._rootContext._non_existent():
00158 return False
00159 return True
00160 except:
00161 self.__print_exception()
00162 return False
00163
00164 return False
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 def bind(self, name_list, obj, force=None):
00214 if force is None :
00215 force = True
00216
00217 try:
00218 self._rootContext.bind(name_list, obj)
00219 except CosNaming.NamingContext.NotFound:
00220 if force:
00221 self.bindRecursive(self._rootContext, name_list, obj)
00222 else:
00223 raise
00224 except CosNaming.NamingContext.CannotProceed, err:
00225 if force:
00226 self.bindRecursive(err.cxt, err.rest_of_name, obj)
00227 else:
00228 raise
00229 except CosNaming.NamingContext.AlreadyBound:
00230 self._rootContext.rebind(name_list, obj)
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 def bindByString(self, string_name, obj, force=True):
00258 self.bind(self.toName(string_name), obj, force)
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 def bindRecursive(self, context, name_list, obj):
00297 length = len(name_list)
00298 cxt = context
00299 for i in range(length):
00300 if i == length -1:
00301 try:
00302 cxt.bind(self.subName(name_list, i, i), obj)
00303 except CosNaming.NamingContext.AlreadyBound:
00304 cxt.rebind(self.subName(name_list, i, i), obj)
00305 return
00306 else:
00307 if self.objIsNamingContext(cxt):
00308 cxt = self.bindOrResolveContext(cxt,self.subName(name_list, i, i))
00309 else:
00310 raise CosNaming.NamingContext.CannotProceed(cxt, self.subName(name_list, i))
00311 return
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 def rebind(self, name_list, obj, force=True):
00339 if force is None:
00340 force = True
00341
00342 try:
00343 self._rootContext.rebind(name_list, obj)
00344
00345 except CosNaming.NamingContext.NotFound:
00346 if force:
00347 self.rebindRecursive(self._rootContext, name_list, obj)
00348 else:
00349 self.__print_exception()
00350 raise
00351
00352 except CosNaming.NamingContext.CannotProceed, err:
00353 if force:
00354 self.rebindRecursive(err.cxt, err,rest_of_name, obj)
00355 else:
00356 self.__print_exception()
00357 raise
00358
00359 return
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 def rebindByString(self, string_name, obj, force=True):
00386 self.rebind(self.toName(string_name), obj, force)
00387
00388 return
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 def rebindRecursive(self, context, name_list, obj):
00416 length = len(name_list)
00417 for i in range(length):
00418 if i == length - 1:
00419 context.rebind(self.subName(name_list, i, i), obj)
00420 return
00421 else:
00422 if self.objIsNamingContext(context):
00423 try:
00424 context = context.bind_new_context(self.subName(name_list, i, i))
00425 except CosNaming.NamingContext.AlreadyBound:
00426 obj_ = context.resolve(self.subName(name_list, i, i))
00427 context = obj_._narrow(CosNaming.NamingContext)
00428 else:
00429 raise CosNaming.NamingContext.CannotProceed(context, self.subName(name_list, i))
00430 return
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 def bindContext(self, name, name_cxt, force=True):
00458 if isinstance(name, basestring):
00459 self.bind(self.toName(name), name_cxt, force)
00460 else:
00461 self.bind(name, name_cxt, force)
00462 return
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 def bindContextRecursive(self, context, name_list, name_cxt):
00484 self.bindRecursive(context, name_list, name_cxt)
00485 return
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 def rebindContext(self, name, name_cxt, force=True):
00512 if isinstance(name, basestring):
00513 self.rebind(self.toName(name), name_cxt, force)
00514 else:
00515 self.rebind(name, name_cxt, force)
00516 return
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 def rebindContextRecursive(self, context, name_list, name_cxt):
00537 self.rebindRecursive(context, name_list, name_cxt)
00538 return
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 def resolve(self, name):
00569 if isinstance(name, basestring):
00570 name_ = self.toName(name)
00571 else:
00572 name_ = name
00573
00574 try:
00575 obj = self._rootContext.resolve(name_)
00576 return obj
00577 except CosNaming.NamingContext.NotFound, ex:
00578 self.__print_exception()
00579 return None
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 def unbind(self, name):
00610 if isinstance(name, basestring):
00611 name_ = self.toName(name)
00612 else:
00613 name_ = name
00614
00615 try:
00616 self._rootContext.unbind(name_)
00617 except:
00618 self.__print_exception()
00619
00620 return
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638 def newContext(self):
00639 return self._rootContext.new_context()
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 def bindNewContext(self, name, force=True):
00669 if force is None:
00670 force = True
00671
00672 if isinstance(name, basestring):
00673 name_ = self.toName(name)
00674 else:
00675 name_ = name
00676
00677 try:
00678 return self._rootContext.bind_new_context(name_)
00679 except CosNaming.NamingContext.NotFound:
00680 if force:
00681 self.bindRecursive(self._rootContext, name_, self.newContext())
00682 else:
00683 self.__print_exception()
00684 raise
00685 except CosNaming.NamingContext.CannotProceed, err:
00686 if force:
00687 self.bindRecursive(err.cxt, err.rest_of_name, self.newContext())
00688 else:
00689 self.__print_exception()
00690 raise
00691 return None
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725 def destroy(self, context):
00726 context.destroy()
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 def destroyRecursive(self, context):
00749 cont = True
00750 bl = []
00751 bi = 0
00752 bl, bi = context.list(self._blLength)
00753 while cont:
00754 for i in range(len(bl)):
00755 if bl[i].binding_type == CosNaming.ncontext:
00756 obj = context.resolve(bl[i].binding_name)
00757 next_context = obj._narrow(CosNaming.NamingContext)
00758
00759 self.destroyRecursive(next_context)
00760 context.unbind(bl[i].binding_name)
00761 next_context.destroy()
00762 elif bl[i].binding_type == CosNaming.nobject:
00763 context.unbind(bl[i].binding_name)
00764 else:
00765 assert(0)
00766 if CORBA.is_nil(bi):
00767 cont = False
00768 else:
00769 bi.next_n(self._blLength, bl)
00770
00771 if not (CORBA.is_nil(bi)):
00772 bi.destroy()
00773 return
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 def clearAll(self):
00788 self.destroyRecursive(self._rootContext)
00789 return
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 def list(self, name_cxt, how_many, rbl, rbi):
00807 bl, bi = name_cxt.list(how_many)
00808
00809 for i in bl:
00810 rbl.append(bl)
00811
00812 rbi.append(bi)
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 def toString(self, name_list):
00836 if len(name_list) == 0:
00837 raise CosNaming.NamingContext.InvalidName
00838
00839 slen = self.getNameLength(name_list)
00840 string_name = [""]
00841 self.nameToString(name_list, string_name, slen)
00842
00843 return string_name[0]
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862 def toName(self, sname):
00863 if not sname:
00864 raise CosNaming.NamingContext.InvalidName
00865
00866 string_name = sname
00867 name_comps = []
00868
00869 nc_length = 0
00870 nc_length = self.split(string_name, "/", name_comps)
00871
00872 if not (nc_length > 0):
00873 raise CosNaming.NamingContext.InvalidName
00874
00875 name_list = [CosNaming.NameComponent("","") for i in range(nc_length)]
00876
00877 for i in range(nc_length):
00878 pos = string.rfind(name_comps[i][0:],".")
00879 if pos == -1:
00880 name_list[i].id = name_comps[i]
00881 name_list[i].kind = ""
00882 else:
00883 name_list[i].id = name_comps[i][0:pos]
00884 name_list[i].kind = name_comps[i][(pos+1):]
00885
00886 return name_list
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 def toUrl(self, addr, string_name):
00908 return self._rootContext.to_url(addr, string_name)
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930 def resolveStr(self, string_name):
00931 return self.resolve(self.toName(string_name))
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958 def bindOrResolve(self, context, name_list, obj):
00959 try:
00960 context.bind_context(name_list, obj)
00961 return obj
00962 except CosNaming.NamingContext.AlreadyBound:
00963 obj = context.resolve(name_list)
00964 return obj
00965 return CORBA.Object._nil
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989 def bindOrResolveContext(self, context, name_list, new_context=None):
00990 if new_context is None:
00991 new_cxt = self.newContext()
00992 else:
00993 new_cxt = new_context
00994
00995 obj = self.bindOrResolve(context, name_list, new_cxt)
00996 return obj._narrow(CosNaming.NamingContext)
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012 def getNameServer(self):
01013 return self._nameServer
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029 def getRootContext(self):
01030 return self._rootContext
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 def objIsNamingContext(self, obj):
01048 nc = obj._narrow(CosNaming.NamingContext)
01049 if CORBA.is_nil(nc):
01050 return False
01051 else:
01052 return True
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070 def nameIsNamingContext(self, name_list):
01071 return self.objIsNamingContext(self.resolve(name_list))
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092 def subName(self, name_list, begin, end = None):
01093 if end is None or end < 0:
01094 end = len(name_list) - 1
01095
01096 sub_len = end - (begin -1)
01097 objId = ""
01098 kind = ""
01099
01100 sub_name = []
01101 for i in range(sub_len):
01102 sub_name.append(name_list[begin + i])
01103
01104 return sub_name
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126 def nameToString(self, name_list, string_name, slen):
01127 for i in range(len(name_list)):
01128 for id_ in name_list[i].id:
01129 if id_ == "/" or id_ == "." or id_ == "\\":
01130 string_name[0] += "\\"
01131 string_name[0] += id_
01132
01133 if name_list[i].id == "" or name_list[i].kind != "":
01134 string_name[0] += "."
01135
01136 for kind_ in name_list[i].kind:
01137 if kind_ == "/" or kind_ == "." or kind_ == "\\":
01138 string_name[0] += "\\"
01139 string_name[0] += kind_
01140
01141 string_name[0] += "/"
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161 def getNameLength(self, name_list):
01162 slen = 0
01163
01164 for i in range(len(name_list)):
01165 for id_ in name_list[i].id:
01166 if id_ == "/" or id_ == "." or id_ == "\\":
01167 slen += 1
01168 slen += 1
01169 if name_list[i].id == "" or name_list[i].kind == "":
01170 slen += 1
01171
01172 for kind_ in name_list[i].kind:
01173 if kind_ == "/" or kind_ == "." or kind_ == "\\":
01174 slen += 1
01175 slen += 1
01176
01177 slen += 1
01178
01179 return slen
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198 def split(self, input, delimiter, results):
01199 delim_size = len(delimiter)
01200 found_pos = 0
01201 begin_pos = 0
01202 pre_pos = 0
01203 substr_size = 0
01204
01205 if input[0:delim_size] == delimiter:
01206 begin_pos = delim_size
01207 pre_pos = delim_size
01208
01209 while 1:
01210 found_pos = string.find(input[begin_pos:],delimiter)
01211 if found_pos == -1:
01212 results.append(input[pre_pos:])
01213 break
01214
01215 if found_pos > 0 and input[found_pos + begin_pos - 1] == "\\":
01216 begin_pos += found_pos + delim_size
01217 else:
01218 substr_size = found_pos + (begin_pos - pre_pos)
01219 if substr_size > 0:
01220 results.append(input[pre_pos:(pre_pos+substr_size)])
01221 begin_pos += found_pos + delim_size
01222 pre_pos = begin_pos
01223
01224 return len(results)
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238 def __print_exception(self):
01239 if sys.version_info[0:3] >= (2, 4, 0):
01240 print traceback.format_exc()
01241 else:
01242 _exc_list = traceback.format_exception(*sys.exc_info())
01243 _exc_str = "".join(_exc_list)
01244 print _exc_str
01245
01246 return