Changeset 2229
- Timestamp:
- 06/11/08 15:43:06 (3 months ago)
- Files:
-
- trunk/framework-plugin/src/com/tresys/framework/plugin/editor/action/AddCustomPolicy.java (modified) (2 diffs)
- trunk/framework-plugin/src/com/tresys/framework/plugin/editor/policy/graphic/commands/AddCustomization.java (added)
- trunk/framework-plugin/src/com/tresys/framework/plugin/editor/policy/graphic/commands/ShapeDeleteCommand.java (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/framework-plugin/src/com/tresys/framework/plugin/editor/action/AddCustomPolicy.java
r2155 r2229 10 10 package com.tresys.framework.plugin.editor.action; 11 11 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;19 12 import 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;35 13 import 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; 14 import com.tresys.framework.plugin.editor.policy.graphic.commands.AddCustomization; 43 15 44 16 … … 65 37 public void run () 66 38 { 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 (); 172 41 } 173 42 } trunk/framework-plugin/src/com/tresys/framework/plugin/editor/policy/graphic/commands/ShapeDeleteCommand.java
r2199 r2229 10 10 package com.tresys.framework.plugin.editor.policy.graphic.commands; 11 11 12 import java.io.BufferedReader; 13 import java.io.File; 14 import java.io.FileNotFoundException; 15 import java.io.FileReader; 16 import java.io.FileWriter; 17 import java.io.IOException; 12 18 import java.util.ArrayList; 13 19 import java.util.Collection; 14 20 import java.util.Iterator; 15 21 import java.util.List; 16 22 import java.util.Map; 23 24 import org.eclipse.core.resources.IFile; 17 25 import org.eclipse.gef.commands.Command; 18 26 import org.eclipse.jface.dialogs.MessageDialog; … … 20 28 import org.eclipse.ui.PlatformUI; 21 29 30 import com.tresys.framework.compiler.custom.CustomUtil; 22 31 import com.tresys.framework.compiler.policy.NetworkResource; 23 32 import com.tresys.framework.compiler.systemResources.LabeledNetworkResource; 24 33 import com.tresys.framework.compiler.systemResources.SystemResource; 25 34 import com.tresys.framework.plugin.builder.ENetworkConfig; 35 import com.tresys.framework.plugin.builder.SELinuxSystem; 26 36 import com.tresys.framework.plugin.editor.policy.graphic.model.Connection; 27 37 import com.tresys.framework.plugin.editor.policy.graphic.model.ControlResourceShape; … … 32 42 import com.tresys.framework.plugin.editor.policy.graphic.model.PolicyDiagram; 33 43 import com.tresys.framework.plugin.editor.policy.graphic.model.Shape; 44 import com.tresys.slide.plugin.nature.PolicyXMLContainer; 45 import com.tresys.slide.plugin.nature.SLIDEProjectNature; 46 import com.tresys.slide.utility.policyxmlparser.IPolicyPartCreator; 47 import com.tresys.slide.utility.policyxmlparser.MacroNode; 48 import com.tresys.slide.utility.policyxmlparser.MacroParseException; 49 import com.tresys.slide.utility.policyxmlparser.MacroParser; 34 50 35 51 /** … … 54 70 private Collection m_controlResources = null; 55 71 72 private StringBuffer mCustomization = new StringBuffer(); 73 56 74 /** 57 75 * Create a command that will remove the shape from its parent. … … 103 121 m_connections.addAll(cons); 104 122 m_connections.addAll(getInnerConnections(cons)); 123 124 if (m_child.hasCustomization () ) 125 { 126 saveCustomPolicy (); 127 } 105 128 106 129 if (m_child instanceof DomainShape) … … 241 264 addNetworkConfig (); 242 265 } 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 } 243 447 } 244 448
