📄 cospropertyservice_propertysetdef_impl.erl
字号:
%%--------------------------------------------------------------------%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %% $Id$%%%%-----------------------------------------------------------------%% File: CosPropertyService_PropertySetDef_impl.erl%% Creation date: 2000-06-12%% Modified:%%%%-----------------------------------------------------------------%% README: %% (1) The OMG specification states that a property name may not%% be an empty string (""). We may restrict this further%% but there is no reason for that.%%------------------------------------------------------------------module('CosPropertyService_PropertySetDef_impl').%%----------------------------------------------------------------------%% Include files%%-----------------------------------------------------------------------include_lib("orber/include/corba.hrl").-include_lib("orber/src/orber_iiop.hrl").-include_lib("cosProperty/include/CosPropertyService.hrl").-include("cosProperty.hrl").%%----------------------------------------------------------------------%% External exports%%----------------------------------------------------------------------%% Mandatory callbacks-export([init/1, terminate/2, code_change/3]).%% Inherrit from CosPropertyService::PropertySet-export([define_property/4, define_properties/3, get_number_of_properties/2, get_all_property_names/3, get_property_value/3, get_properties/3, get_all_properties/3, delete_property/3, delete_properties/3, delete_all_properties/2, is_property_defined/3]).%% CosPropertyService::PropertySetDef-export([get_allowed_property_types/2, get_allowed_properties/2, define_property_with_mode/5, define_properties_with_modes/3, get_property_mode/3, get_property_modes/3, set_property_mode/4, set_property_modes/3]).%%----------------------------------------------------------------------%% Internal exports%%-----------------------------------------------------------------------export([dump/0]).%%----------------------------------------------------------------------%% Records%%-----------------------------------------------------------------------record(state, {dbKey, defaultMode, okTypes, okProperties, myType}).%%----------------------------------------------------------------------%% Macros%%-----------------------------------------------------------------------define(create_InitState(K, D, AT, AP, MT), #state{dbKey = K, defaultMode = D, okTypes = AT, okProperties = AP, myType = MT}).%% Selectors-define(get_DBKey(S), S#state.dbKey).-define(get_DefaultMode(S), S#state.defaultMode).-define(get_okTypes(S), S#state.okTypes).-define(get_okProperties(S), S#state.okProperties).%% MISC-define(is_NotSetDef(S), S#state.myType =/= ?PropertySetDef).-define(no_PropertyLimits(S), S#state.okProperties == []).-define(no_TypeLimits(S), S#state.okTypes == []).-define(is_NotStatic(S), binary(S#state.dbKey)).%% Fun:s-define(Local2Property, fun({N,V,_M}) -> #'CosPropertyService_Property'{property_name = N, property_value = V} end).-define(Local2Names, fun({N,_V,_M}) -> N end).-define(MemberName(N), fun(R) -> case R of Property when record(R, 'CosPropertyService_Property') -> Property#'CosPropertyService_Property'.property_name == N; PropertyDef when record(R, 'CosPropertyService_PropertyDef') -> PropertyDef#'CosPropertyService_PropertyDef'.property_name == N; _-> false end end).%%======================================================================%% External functions%%======================================================================%%----------------------------------------------------------------------%% Function : init/1%% Description: Initiates the server%% Returns : {ok, State} |%% {ok, State, Timeout} |%% ignore |%% {stop, Reason}%%----------------------------------------------------------------------init({DefMode, AllowedTypes, AllowedProperties, InitProperties, MyType}) -> Key = term_to_binary({now(), node()}), _F = ?write_function(#oe_CosPropertyService{key=Key, properties=InitProperties}), write_result(mnesia:transaction(_F)), {ok, ?create_InitState(Key, DefMode, AllowedTypes, AllowedProperties, MyType)};init({static, DefMode, AllowedTypes, AllowedProperties, InitProperties, MyType}) -> {ok, ?create_InitState(InitProperties, DefMode, AllowedTypes, AllowedProperties, MyType)}.%%---------------------------------------------------------------------%%% Function : terminate%% Description: Shutdown the server%% Returns : any (ignored by gen_server)%%----------------------------------------------------------------------terminate(_Reason, State) when ?is_NotStatic(State) -> _DF = ?delete_function({oe_CosPropertyService, ?get_DBKey(State)}), catch write_result(mnesia:transaction(_DF)), ok;terminate(_Reason, _State) -> ok.%%---------------------------------------------------------------------%%% Function : code_change%% Description: Convert process state when code is changed%% Returns : {ok, State}%%----------------------------------------------------------------------code_change(_OldVsn, State, _Extra) -> {ok, State}.%%----------------------------------------------------------------------%% Interface CosPropertyService::PropertySet%%----------------------------------------------------------------------%%---------------------------------------------------------------------%%% Function : define_property%% Arguments : %% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------define_property(_, _, "", _) -> corba:raise(#'CosPropertyService_InvalidPropertyName'{});define_property(_OE_This, State, Name, Value) when ?is_NotStatic(State) -> evaluate_property_data(State, Value, Name), _DF = fun() -> case mnesia_read(State) of {'EXCEPTION', E} -> {'EXCEPTION', E}; X -> case catch update_property(X, Name, value, Value, ?get_DefaultMode(State)) of {'EXCEPTION', E} when record(E, 'CosPropertyService_PropertyNotFound') -> mnesia_write(State, [{Name, Value, ?get_DefaultMode(State)}|X]); {'EXCEPTION', E} -> {'EXCEPTION', E}; NewProperties -> mnesia_write(State, NewProperties) end end end, {reply, mnesia_transaction(_DF), State};define_property(_OE_This, State, Name, Value) -> evaluate_property_data(State, Value, Name), X = ?get_DBKey(State), case catch update_property(X, Name, value, Value, ?get_DefaultMode(State)) of {'EXCEPTION', E} when record(E, 'CosPropertyService_PropertyNotFound') -> corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO}); {'EXCEPTION', E} -> corba:raise(E); _NewProperties -> corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO}) end.%%---------------------------------------------------------------------%%% Function : get_property_value%% Arguments : %% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------get_property_value(_, _, "") -> corba:raise(#'CosPropertyService_InvalidPropertyName'{});get_property_value(_OE_THIS, State, Name) -> X = lookup_table(?get_DBKey(State)), {reply, find_property(X, Name, value), State}.%%---------------------------------------------------------------------%%% Function : delete_property%% Arguments : %% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------delete_property(_, _, "") -> corba:raise(#'CosPropertyService_InvalidPropertyName'{});delete_property(_OE_THIS, State, Name) when ?is_NotStatic(State) -> _DF = fun() -> case mnesia_read(State) of {'EXCEPTION', E} -> {'EXCEPTION', E}; X -> case catch remove_property(X, Name) of {'EXCEPTION', E} -> {'EXCEPTION', E}; NewProperties -> mnesia_write(State, NewProperties) end end end, {reply, mnesia_transaction(_DF), State};delete_property(_OE_THIS, State, Name) -> X = lookup_table(?get_DBKey(State)), %% Check the properties; must raise an exception. remove_property(X, Name), %% Something is not correct. corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO}).%%---------------------------------------------------------------------%%% Function : define_properties%% Arguments : %% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------define_properties(_OE_THIS, State, PropertySeq) when ?is_NotStatic(State) -> {OKProperties, Exc} = evaluate_properties_data(State, PropertySeq), _DF = fun() -> case mnesia_read(State) of {'EXCEPTION', E} -> {'EXCEPTION', E}; X -> case catch define_properties_helper(State, OKProperties, X, Exc) of {'EXCEPTION', E} -> {'EXCEPTION', E}; NewProperties -> mnesia_write(State, NewProperties) end end end, {reply, mnesia_transaction(_DF), State};define_properties(_OE_THIS, State, PropertySeq) -> {OKProperties, Exc} = evaluate_properties_data(State, PropertySeq), X = lookup_table(?get_DBKey(State)), case define_properties_helper(State, OKProperties, X, Exc) of {'EXCEPTION', E} -> corba:raise(E); _ -> corba:raise(#'INTERNAL'{completion_status=?COMPLETED_NO}) end.define_properties_helper(_State, [], NewProperties, []) -> %% No exceptions, insert the properties. NewProperties;define_properties_helper(_State, [], _, MultipleExceptions) -> {'EXCEPTION', #'CosPropertyService_MultipleExceptions'{exceptions = MultipleExceptions}};define_properties_helper(State, [#'CosPropertyService_Property' {property_name = Name, property_value = Value}|T], Properties, Exc) -> case catch update_property(Properties, Name, value, Value, ?get_DefaultMode(State)) of {'EXCEPTION', E} when record(E, 'CosPropertyService_PropertyNotFound') -> define_properties_helper(State, T, [{Name, Value, ?get_DefaultMode(State)}|Properties], Exc); {'EXCEPTION', E} -> define_properties_helper(State, T, Properties, [#'CosPropertyService_PropertyException' {reason = remap_exception(E), failing_property_name = Name}|Exc]); NewProperties -> define_properties_helper(State, T, NewProperties, Exc) end.%%---------------------------------------------------------------------%%% Function : get_number_of_properties%% Arguments : -%% Description: Returns the number of properties currently associated%% with this object.%% Returns : {ok, ulong(), State}%%----------------------------------------------------------------------get_number_of_properties(_OE_THIS, State) -> X = lookup_table(?get_DBKey(State)), {reply, length(X), State}.%%---------------------------------------------------------------------%%% Function : get_all_property_names%% Arguments : %% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------get_all_property_names(_OE_THIS, State, Max) -> X = lookup_table(?get_DBKey(State)), {reply, get_all_property_names_helper(X, [], Max), State}.get_all_property_names_helper([], Acc, _) -> %% There are no more properties; return a nil-object refernce. {ok, Acc, corba:create_nil_objref()};get_all_property_names_helper(Left, Acc, 0) -> %% There are more properties; create Name Iterartor. PropertyNames = lists:map(?Local2Names, Left), {ok, Acc, cosProperty:start_PropertyNamesIterator(PropertyNames)};get_all_property_names_helper([{Name, _, _}|T], Acc, No) -> get_all_property_names_helper(T, [Name|Acc], No-1).%%---------------------------------------------------------------------%%% Function : get_properties%% Arguments : A list of property names, i.e., string()%% Description: %% Returns : {ok, State}%%----------------------------------------------------------------------get_properties(_OE_THIS, State, PropertyNames) -> X = lookup_table(?get_DBKey(State)), {reply, locate_names(PropertyNames, X, true, []), State}.locate_names([], _, AllOK, Acc) -> {AllOK, Acc};locate_names([""|T], X, _AllOK, Acc) ->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -