Changeset 2229

Show
Ignore:
Timestamp:
06/11/08 15:43:06 (3 months ago)
Author:
apatel
Message:

Changed adding customization from Action to GEF Command to disable Undo stack upon the customization.
Also, added support to be able to delete the customized shape that saves the customized policy to be able to undo the operation putting the customized policy back in place.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/framework-plugin/src/com/tresys/framework/plugin/editor/action/AddCustomPolicy.java

    r2155 r2229  
    1010package com.tresys.framework.plugin.editor.action; 
    1111 
    12 import java.io.ByteArrayInputStream; 
    13 import java.io.File; 
    14 import java.io.OutputStream; 
    15  
    16 import org.eclipse.core.resources.IFile; 
    17 import org.eclipse.core.resources.IResource; 
    18 import org.eclipse.core.runtime.CoreException; 
    1912import org.eclipse.jface.action.Action; 
    20 import org.eclipse.jface.dialogs.ErrorDialog; 
    21 import org.eclipse.jface.dialogs.MessageDialog; 
    22 import org.eclipse.jface.viewers.IStructuredSelection; 
    23 import org.eclipse.jface.viewers.StructuredSelection; 
    24 import org.eclipse.swt.widgets.Shell; 
    25 import org.eclipse.ui.PartInitException; 
    26 import org.eclipse.ui.ide.IDE; 
    27  
    28 import com.tresys.framework.compiler.custom.ICustomPolicy; 
    29 import com.tresys.framework.compiler.linkage.Linkage; 
    30 import com.tresys.framework.compiler.linkage.libselinuxjava_plugin; 
    31 import com.tresys.framework.compiler.policy.Component; 
    32 import com.tresys.framework.compiler.translator.SELinuxPolicy; 
    33 import com.tresys.framework.plugin.SEFramework_Plugin; 
    34 import com.tresys.framework.plugin.builder.SELinuxSystem; 
    3513import com.tresys.framework.plugin.editor.policy.graphic.GraphicPolicyEditor; 
    36 import com.tresys.framework.plugin.editor.policy.graphic.GraphicPolicyMetaVisitor; 
    37 import com.tresys.framework.plugin.editor.policy.graphic.model.Connection; 
    38 import com.tresys.framework.plugin.editor.policy.graphic.model.Shape; 
    39 import com.tresys.framework.plugin.editor.policy.graphic.parts.AbstractConnectionEditPart; 
    40 import com.tresys.framework.plugin.editor.policy.graphic.parts.PolicyEditPart; 
    41 import com.tresys.slide.plugin.editors.module.ModuleEditor; 
    42 import com.tresys.slide.plugin.editors.module.ModuleEditorInput; 
     14import com.tresys.framework.plugin.editor.policy.graphic.commands.AddCustomization; 
    4315 
    4416 
     
    6537        public void run () 
    6638        { 
    67                 Object unknown_selection = mEditor.getViewer ().getSelection (); 
    68                 if(!( unknown_selection instanceof StructuredSelection)) 
    69                         return; 
    70                 IStructuredSelection selection = (StructuredSelection)unknown_selection; 
    71                 Object selected_item = selection.getFirstElement (); 
    72  
    73                 /** 
    74                  * Warn user ...! 
    75                  */ 
    76                 Shell shell = SEFramework_Plugin.getDefault ().getWorkbench ().getActiveWorkbenchWindow ().getShell (); 
    77                 boolean OK = MessageDialog.openConfirm (shell, Messages.custom_policy_dialog_title, Messages.custom_policy_add_warning_message); 
    78  
    79                 if( ! OK ) 
    80                         return; 
    81                  
    82                 try 
    83                 { 
    84                         IFile []files = mEditor.getSystem ().getCustomModuleFiles (); 
    85                          
    86                         if( files == null ) 
    87                         { 
    88                                 MessageDialog.openError (shell, Messages.custom_policy_dialog_title, Messages.custom_policy_file_error); //$NON-NLS-1$ 
    89                                 return; 
    90                         } 
    91                                  
    92                         if( files[ModuleEditorInput.IF_EDITOR_INDEX].exists () ) 
    93                         { 
    94                                 /** 
    95                                  * Get the selection, get policy of the selection  
    96                                  * and get the file system resource for editor input. 
    97                                  */ 
    98                                 final OutputStream stream = getPolicy (selected_item); 
    99                                 if( stream == null ) 
    100                                 { 
    101                                         return; 
    102                                 } 
    103                                  
    104                                 ByteArrayInputStream input = new ByteArrayInputStream(stream.toString ().getBytes ()); 
    105                                 files[0].appendContents (input, IResource.KEEP_HISTORY, null); 
    106                         } 
    107                         /** 
    108                          * provide input to editor and open up the editor. 
    109                          */ 
    110  
    111                         ModuleEditorInput fileInput = new ModuleEditorInput (files[0]);                  
    112  
    113                         IDE.openEditor (mEditor.getSite ().getPage (), fileInput, ModuleEditor.ID, true); 
    114                 } 
    115                 catch(PartInitException pe) 
    116                 { 
    117                         ErrorDialog.openError (shell,Messages.custom_policy_dialog_title,pe.getMessage (), pe.getStatus ()); 
    118                 } 
    119                 catch(CoreException ce) 
    120                 { 
    121                         ErrorDialog.openError (shell,Messages.custom_policy_dialog_title,ce.getMessage (), ce.getStatus ()); //$NON-NLS-1$ 
    122                 } 
    123         } 
    124          
    125         private OutputStream getPolicy(Object selected_item) 
    126         { 
    127                 final SELinuxSystem system = mEditor.getSystem(); 
    128                 Linkage linkage = system.getLinkage (); 
    129                  
    130                 IFile fileContextsFile = system.getFileContextFile (); 
    131                 File fileContexts = null; 
    132                 if (fileContextsFile.exists())  
    133                 { 
    134                         if (libselinuxjava_plugin.getDefault ().isLoaded ()) 
    135                                 fileContexts = fileContextsFile.getRawLocation().toFile(); 
    136                 } 
    137  
    138                 SELinuxPolicy policy = new SELinuxPolicy(linkage, system.getNetworkConfig (), system.getName (), fileContexts, true); 
    139                  
    140                 if( selected_item instanceof PolicyEditPart) 
    141                 { 
    142                         PolicyEditPart policy_part = (PolicyEditPart)selected_item; 
    143                         Object model = policy_part.getModel (); 
    144                         if( model instanceof Shape ) 
    145                         { 
    146                                 Shape shape = (Shape)model; 
    147                                 Component comp = shape.getComponent (); 
    148                                 if( comp instanceof ICustomPolicy) 
    149                                 { 
    150                                         comp.Accept (policy); 
    151                                         // generated policy 
    152                                         OutputStream p = policy.GetInterfaces (); 
    153                                         return p; 
    154                                 } 
    155                         } 
    156                 } 
    157                 else if( selected_item instanceof AbstractConnectionEditPart ) 
    158                 { 
    159                         AbstractConnectionEditPart part = (AbstractConnectionEditPart)selected_item; 
    160                         Object model = part.getModel (); 
    161                         if( model instanceof Connection) 
    162                         { 
    163                                 Connection con = (Connection)model; 
    164                                 GraphicPolicyMetaVisitor meta_visitor = new GraphicPolicyMetaVisitor(policy); 
    165                                 con.Accept (meta_visitor); 
    166                                 // generated policy 
    167                                 OutputStream p = policy.GetInterfaces (); 
    168                                 return p; 
    169                         } 
    170                 } 
    171                 return null; 
     39                AddCustomization custom_command = new AddCustomization(mEditor); 
     40                custom_command.execute (); 
    17241        } 
    17342} 
  • trunk/framework-plugin/src/com/tresys/framework/plugin/editor/policy/graphic/commands/ShapeDeleteCommand.java

    r2199 r2229  
    1010package com.tresys.framework.plugin.editor.policy.graphic.commands; 
    1111 
     12import java.io.BufferedReader; 
     13import java.io.File; 
     14import java.io.FileNotFoundException; 
     15import java.io.FileReader; 
     16import java.io.FileWriter; 
     17import java.io.IOException; 
    1218import java.util.ArrayList; 
    1319import java.util.Collection; 
    1420import java.util.Iterator; 
    1521import java.util.List; 
    16  
     22import java.util.Map; 
     23 
     24import org.eclipse.core.resources.IFile; 
    1725import org.eclipse.gef.commands.Command; 
    1826import org.eclipse.jface.dialogs.MessageDialog; 
     
    2028import org.eclipse.ui.PlatformUI; 
    2129 
     30import com.tresys.framework.compiler.custom.CustomUtil; 
    2231import com.tresys.framework.compiler.policy.NetworkResource; 
    2332import com.tresys.framework.compiler.systemResources.LabeledNetworkResource; 
    2433import com.tresys.framework.compiler.systemResources.SystemResource; 
    2534import com.tresys.framework.plugin.builder.ENetworkConfig; 
     35import com.tresys.framework.plugin.builder.SELinuxSystem; 
    2636import com.tresys.framework.plugin.editor.policy.graphic.model.Connection; 
    2737import com.tresys.framework.plugin.editor.policy.graphic.model.ControlResourceShape; 
     
    3242import com.tresys.framework.plugin.editor.policy.graphic.model.PolicyDiagram; 
    3343import com.tresys.framework.plugin.editor.policy.graphic.model.Shape; 
     44import com.tresys.slide.plugin.nature.PolicyXMLContainer; 
     45import com.tresys.slide.plugin.nature.SLIDEProjectNature; 
     46import com.tresys.slide.utility.policyxmlparser.IPolicyPartCreator; 
     47import com.tresys.slide.utility.policyxmlparser.MacroNode; 
     48import com.tresys.slide.utility.policyxmlparser.MacroParseException; 
     49import com.tresys.slide.utility.policyxmlparser.MacroParser; 
    3450 
    3551/** 
     
    5470        private Collection m_controlResources = null; 
    5571         
     72        private StringBuffer mCustomization = new StringBuffer(); 
     73         
    5674        /** 
    5775         * Create a command that will remove the shape from its parent. 
     
    103121                m_connections.addAll(cons); 
    104122                m_connections.addAll(getInnerConnections(cons)); 
     123                 
     124                if (m_child.hasCustomization () ) 
     125                { 
     126                        saveCustomPolicy (); 
     127                } 
    105128 
    106129                if (m_child instanceof DomainShape) 
     
    241264                        addNetworkConfig (); 
    242265                } 
     266                loadCustomPolicy (); 
     267        } 
     268         
     269        private void loadCustomPolicy() 
     270        { 
     271                if( mCustomization.length () <= 0) 
     272                        return; 
     273                 
     274                final SELinuxSystem system = m_child.getSystem (); 
     275                final IFile []files = system.getCustomModuleFiles (); 
     276                 
     277                if( files == null ) 
     278                { 
     279                        return; 
     280                } 
     281                 
     282                SLIDEProjectNature nat = SLIDEProjectNature.getNature(system.getProject ()); 
     283                 
     284                PolicyXMLContainer policyXML = nat.getPolicyXML (system.getFolder ()); 
     285                if ( policyXML == null ) 
     286                { 
     287                        return; 
     288                } 
     289                 
     290                IPolicyPartCreator part_creator = policyXML.getCreator (); 
     291                if( part_creator == null ) 
     292                { 
     293                        return; 
     294                } 
     295 
     296                try 
     297                { 
     298                        final File interfacefile = files[0].getLocation ().toFile (); 
     299                        FileWriter writer = new FileWriter (interfacefile, true); 
     300                        writer.write (mCustomization.toString ()); 
     301                        writer.flush (); 
     302                        writer.close (); 
     303                } 
     304                catch(IOException ie) 
     305                { 
     306                        ie.printStackTrace (); 
     307                } 
     308        } 
     309 
     310        private void saveCustomPolicy() 
     311        { 
     312                        final SELinuxSystem system = m_child.getSystem (); 
     313                        final IFile []files = system.getCustomModuleFiles (); 
     314                        final List lines_to_remove = new ArrayList(); 
     315                        final List lines = new ArrayList(); 
     316 
     317                         
     318//                      Shell shell = Display.getCurrent ().getActiveShell (); 
     319                        if( files == null ) 
     320                                return; 
     321                         
     322                        SLIDEProjectNature nat = SLIDEProjectNature.getNature(system.getProject ()); 
     323                         
     324                        PolicyXMLContainer policyXML = nat.getPolicyXML (system.getFolder ()); 
     325                        if ( policyXML == null ) 
     326                                return; 
     327                         
     328                        IPolicyPartCreator part_creator = policyXML.getCreator (); 
     329                        if( part_creator == null ) 
     330                                return; 
     331 
     332                        final File interfacefile = files[0].getLocation ().toFile (); 
     333                         
     334                        Map modified_templates = null; 
     335                         
     336                        try 
     337                        { 
     338                                modified_templates = MacroParser.parseM4File (interfacefile, MacroParser.TEMPLATE, part_creator ); 
     339                        } 
     340                        catch(MacroParseException me) 
     341                        { 
     342                                return; 
     343                        } 
     344                        catch(FileNotFoundException fe) 
     345                        { 
     346                                return; 
     347                        } 
     348                         
     349                        Collection custom_interfafces = null; 
     350 
     351                        if (modified_templates != null && modified_templates.size () > 0 ) 
     352                        { 
     353                                custom_interfafces = CustomUtil.computePossibleNames (m_child.getComponent (), system.getName()); 
     354                        } 
     355                         
     356                        List interface_info = new ArrayList(); 
     357                        if( custom_interfafces != null && custom_interfafces.size () > 0 ) 
     358                        { 
     359                                for( Iterator name_itr = custom_interfafces.iterator (); name_itr.hasNext (); ) 
     360                                { 
     361                                        String name = name_itr.next ().toString (); 
     362                                        if( modified_templates.containsKey (name.toString ()) ) 
     363                                        { 
     364                                                interface_info.add(modified_templates.get (name.toString ())); 
     365                                        } 
     366                                } 
     367                        } 
     368                         
     369                        if( interface_info.size () > 0 ) 
     370                        { 
     371                                try 
     372                                { 
     373                                        BufferedReader reader = new BufferedReader(new FileReader(interfacefile)); 
     374                                        String line; 
     375                                         
     376                                        while ( (line = reader.readLine ()) != null ) 
     377                                        { 
     378                                                lines.add (line); 
     379                                        } 
     380                                        reader.close (); 
     381                                } 
     382                                catch(FileNotFoundException fne) 
     383                                { 
     384                                        return; 
     385                                } 
     386                                catch(IOException ie) 
     387                                { 
     388                                        return; 
     389                                } 
     390                                 
     391                                for( Iterator node_itr = interface_info.iterator (); node_itr.hasNext (); ) 
     392                                { 
     393                                        MacroNode n = (MacroNode)node_itr.next (); 
     394                                        int comment_end_line = -1; 
     395                                        int comment_start_line = -1; 
     396                                         
     397                                        for (int index = 0; index < lines.size () && comment_end_line < 0; index++ ) 
     398                                        { 
     399                                                String st = (String)lines.get(index); 
     400                                                if( st.startsWith ("template")  && st.contains (n.getName ()) ) 
     401                                                { 
     402                                                        comment_end_line = index; 
     403                                                } 
     404                                        } 
     405                                        if( comment_end_line > 1 ) 
     406                                        { 
     407                                                for (int index=comment_end_line-1; index >= 0 && comment_start_line <= 0; index--) 
     408                                                { 
     409                                                        String st = (String)lines.get(index);  
     410                                                        if( ! st.startsWith ("#") ) 
     411                                                                comment_start_line = index; 
     412                                                } 
     413                                        } 
     414                                         
     415                                        if( comment_start_line >= 0 ) 
     416                                        { 
     417                                                for (int index=comment_start_line; index < n.getEndLine (); index++) 
     418                                                { 
     419                                                        lines_to_remove.add (new Integer(index)); 
     420                                                        mCustomization.append (lines.get(index)); 
     421                                                        mCustomization.append ("\n"); 
     422                                                } 
     423                                        } 
     424                                } 
     425                        } 
     426                         
     427                        if( lines_to_remove.size () > 0) 
     428                        { 
     429                                try 
     430                                { 
     431                                        FileWriter writer = new FileWriter(interfacefile, false); 
     432                                        for(int index = 0; index < lines.size (); index++) 
     433                                        { 
     434                                                if( lines_to_remove.contains (new Integer(index)) ) 
     435                                                        continue; 
     436                                                writer.write ((String)lines.get(index)); 
     437                                                writer.write("\n"); 
     438                                        } 
     439                                        writer.flush (); 
     440                                        writer.close (); 
     441                                } 
     442                                catch(IOException ie) 
     443                                { 
     444                                        return; 
     445                                } 
     446                        } 
    243447        } 
    244448